Convert Figma logo to code with AI

guard logoguard-rspec

Guard::RSpec automatically run your specs (much like autotest)

1,311
242
1,311
49

Top Related Projects

RSpec runner and formatters

12,626

A Ruby static code analyzer and formatter, based on the community Ruby style guide.

A library for setting up Ruby objects as test data.

5,871

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites

3,963

Library for stubbing and setting expectations on HTTP requests in Ruby.

Quick Overview

Guard::RSpec is a Guard plugin for RSpec, a popular testing framework for Ruby. It automatically runs your specs when files in your project are modified, providing instant feedback on your code changes and helping to maintain a smooth test-driven development workflow.

Pros

  • Automates the process of running tests, saving time and improving productivity
  • Integrates seamlessly with the Guard ecosystem and other plugins
  • Supports various notification systems for test results
  • Customizable configuration options to fit different project needs

Cons

  • Can be resource-intensive on larger projects with many tests
  • Requires initial setup and configuration
  • May have occasional false positives or negatives due to file system events
  • Learning curve for users new to Guard or automated testing tools

Code Examples

  1. Basic Guard::RSpec setup in a Guardfile:
guard :rspec, cmd: "bundle exec rspec" do
  require "guard/rspec/dsl"
  dsl = Guard::RSpec::Dsl.new(self)

  # RSpec files
  rspec = dsl.rspec
  watch(rspec.spec_helper) { rspec.spec_dir }
  watch(rspec.spec_support) { rspec.spec_dir }
  watch(rspec.spec_files)

  # Ruby files
  ruby = dsl.ruby
  dsl.watch_spec_files_for(ruby.lib_files)
end
  1. Custom command with Spring preloader:
guard :rspec, cmd: "spring rspec" do
  # ... rest of the configuration
end
  1. Configuring notifications:
guard :rspec, cmd: "bundle exec rspec", notification: true, failed_mode: :focus do
  # ... rest of the configuration
end

Getting Started

  1. Add Guard::RSpec to your Gemfile:

    gem 'guard-rspec', require: false
    
  2. Install the gem:

    bundle install
    
  3. Generate a Guardfile:

    bundle exec guard init rspec
    
  4. Start Guard:

    bundle exec guard
    

Now Guard will watch your files and run specs automatically when changes are detected.

Competitor Comparisons

RSpec runner and formatters

Pros of rspec-core

  • Core component of RSpec, providing essential functionality for writing and running tests
  • Highly customizable with a rich set of configuration options
  • Integrates seamlessly with other RSpec libraries (rspec-expectations, rspec-mocks)

Cons of rspec-core

  • Requires manual test execution; no built-in file watching or auto-run features
  • Can be slower for large test suites without additional optimization

Code Comparison

rspec-core:

RSpec.describe "Example" do
  it "does something" do
    expect(true).to be true
  end
end

guard-rspec:

guard :rspec, cmd: "bundle exec rspec" do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
end

Key Differences

  • rspec-core focuses on the core testing functionality, while guard-rspec provides automated test running
  • guard-rspec offers file watching and selective test execution, which rspec-core doesn't provide out-of-the-box
  • rspec-core is essential for writing RSpec tests, whereas guard-rspec is an optional tool for improving the development workflow

Use Cases

  • Use rspec-core for writing and structuring your Ruby tests
  • Use guard-rspec to automate test runs during development, especially for larger projects where quick feedback is crucial
12,626

A Ruby static code analyzer and formatter, based on the community Ruby style guide.

Pros of RuboCop

  • Comprehensive static code analyzer and formatter for Ruby
  • Highly configurable with extensive documentation
  • Large community and frequent updates

Cons of RuboCop

  • Can be overwhelming with numerous rules and configurations
  • May require significant initial setup time
  • Potential for false positives in certain scenarios

Code Comparison

RuboCop configuration example:

AllCops:
  NewCops: enable
  TargetRubyVersion: 2.7

Style/StringLiterals:
  EnforcedStyle: single_quotes

Guard-RSpec configuration example:

guard :rspec, cmd: 'bundle exec rspec' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }
end

Key Differences

  • RuboCop focuses on code style and best practices, while Guard-RSpec automates test running
  • RuboCop is a standalone tool, whereas Guard-RSpec is part of the Guard ecosystem
  • RuboCop provides immediate feedback on code quality, while Guard-RSpec facilitates continuous testing

Use Cases

  • Use RuboCop for enforcing coding standards and identifying potential issues
  • Use Guard-RSpec for automating test runs during development
  • Consider using both tools in conjunction for a comprehensive development workflow

A library for setting up Ruby objects as test data.

Pros of factory_bot

  • Provides a more flexible and powerful way to create test data
  • Supports associations and inheritance, making complex data setup easier
  • Integrates well with various testing frameworks beyond RSpec

Cons of factory_bot

  • Requires more setup and configuration compared to guard-rspec
  • Can potentially slow down tests if overused or not optimized
  • Has a steeper learning curve for beginners

Code Comparison

factory_bot:

FactoryBot.define do
  factory :user do
    name { "John Doe" }
    email { "john@example.com" }
    admin { false }
  end
end

guard-rspec:

guard :rspec, cmd: "bundle exec rspec" do
  require "guard/rspec/dsl"
  dsl = Guard::RSpec::Dsl.new(self)

  watch(dsl.rspec.spec_helper) { "spec" }
  watch(dsl.rspec.spec_files)
end

Key Differences

  • factory_bot focuses on test data generation, while guard-rspec automates test execution
  • factory_bot is used within test files, whereas guard-rspec runs as a separate process
  • factory_bot requires more setup in individual test files, while guard-rspec configuration is centralized

Use Cases

  • Use factory_bot when you need to create complex test data structures
  • Use guard-rspec when you want automated test runs on file changes
  • Consider using both in conjunction for a comprehensive testing setup
5,871

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

Pros of VCR

  • Records and replays HTTP interactions, enabling faster and more reliable tests
  • Allows testing against real external APIs without constant network requests
  • Supports multiple HTTP libraries and test frameworks

Cons of VCR

  • Requires initial setup and configuration for each test suite
  • Can lead to outdated cassettes if external APIs change frequently
  • May not capture all edge cases or real-time API behavior

Code Comparison

VCR usage example:

VCR.use_cassette("example_cassette") do
  response = Net::HTTP.get_response(URI('http://example.com'))
  assert_equal "200", response.code
end

Guard-RSpec usage example:

guard :rspec, cmd: "bundle exec rspec" do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
end

Key Differences

  • VCR focuses on recording and replaying HTTP interactions for testing
  • Guard-RSpec automates running RSpec tests when files change
  • VCR is more suited for API testing, while Guard-RSpec is a general-purpose test runner
  • VCR can be used alongside Guard-RSpec to enhance API testing workflows

Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites

Pros of SimpleCov

  • Provides comprehensive code coverage analysis for Ruby projects
  • Generates detailed HTML reports with line-by-line coverage information
  • Supports multiple test frameworks (RSpec, Test::Unit, Minitest, etc.)

Cons of SimpleCov

  • Focuses solely on code coverage, lacking automated test running capabilities
  • May slightly increase test execution time due to coverage analysis

Code Comparison

SimpleCov setup:

require 'simplecov'
SimpleCov.start

# Your tests go here

Guard-RSpec setup:

guard :rspec, cmd: "bundle exec rspec" do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
end

Key Differences

  • SimpleCov is dedicated to code coverage analysis, while Guard-RSpec focuses on automated test running and file watching
  • Guard-RSpec provides real-time feedback on test results as files change, whereas SimpleCov generates reports after test execution
  • SimpleCov offers more detailed coverage information, including branch coverage and uncovered lines
  • Guard-RSpec integrates with various testing frameworks and tools, not limited to code coverage

Use Cases

  • Use SimpleCov when detailed code coverage analysis is a priority
  • Choose Guard-RSpec for continuous test running and development workflow optimization
  • Consider using both tools in conjunction for a comprehensive testing and coverage solution
3,963

Library for stubbing and setting expectations on HTTP requests in Ruby.

Pros of WebMock

  • Specifically designed for stubbing and mocking HTTP requests
  • Supports a wide range of HTTP libraries (Net::HTTP, HTTPClient, Patron, etc.)
  • Provides detailed request matching and response stubbing capabilities

Cons of WebMock

  • Limited to HTTP request mocking, not a general-purpose testing tool
  • Requires manual setup and teardown in test files
  • May introduce complexity in tests that don't focus on external HTTP interactions

Code Comparison

WebMock example:

stub_request(:get, "https://api.example.com/users")
  .to_return(status: 200, body: '{"users": []}', headers: {'Content-Type' => 'application/json'})

Guard-RSpec example:

guard :rspec, cmd: "bundle exec rspec" do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
end

Key Differences

  1. Purpose: WebMock focuses on HTTP request mocking, while Guard-RSpec automates test execution.
  2. Scope: WebMock is used within test files, Guard-RSpec operates at the project level.
  3. Functionality: WebMock provides request stubbing, Guard-RSpec offers file watching and test running.
  4. Integration: WebMock integrates with test frameworks, Guard-RSpec works with the development workflow.
  5. Usage: WebMock is typically used in individual tests, Guard-RSpec runs continuously during development.

Both tools serve different purposes in the testing ecosystem, with WebMock enhancing test isolation for HTTP interactions and Guard-RSpec improving the development workflow through automated test execution.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Guard::RSpec

Gem Version Build Status Dependency Status Code Climate Coverage Status

Guard::RSpec allows to automatically & intelligently launch specs when files are modified.

  • Compatible with RSpec >2.99 & 3
  • Tested against Ruby 2.2.x, JRuby 9.0.5.0 and Rubinius.

Install

Add the gem to your Gemfile (inside development group):

 gem 'guard-rspec', require: false

Add guard definition to your Guardfile by running this command:

$ bundle exec guard init rspec

Installing with beta versions of RSpec

To install beta versions of RSpec, you need to set versions of all the dependencies, e.g:

gem 'rspec', '= 3.5.0.beta3'
gem 'rspec-core', '= 3.5.0.beta3'
gem 'rspec-expectations', '= 3.5.0.beta3'
gem 'rspec-mocks', '= 3.5.0.beta3'
gem 'rspec-support', '= 3.5.0.beta3'

gem 'guard-rspec', '~> 4.7'

and for Rails projects this also means adding:

gem 'rspec-rails', '= 3.5.0.beta3'

and then running bundle update rspec rspec-core rspec-expectations rspec-mocks rspec-support rspec-rails or just bundle update to update all the gems in your project.

Usage

Please read Guard usage doc.

Guardfile

Guard::RSpec can be adapted to all kinds of projects, some examples:

Standard RubyGem project

guard :rspec, cmd: 'rspec' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }
end

Typical Rails app

guard :rspec, cmd: 'bundle exec rspec' do
  watch('spec/spec_helper.rb')                        { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$})          { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^lib/(.+)\.rb$})                           { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
end

Please read Guard doc for more information about the Guardfile DSL.

Options

Guard::RSpec 4.0 now uses a simpler approach with the new cmd option that let you precisely define which rspec command will be launched on each run. This option is required due to the number of different ways possible to invoke rspec, the template now includes a default that should work for most applications but may not be optimal for all. As example if you want to support Spring with a custom formatter (progress by default) use:

guard :rspec, cmd: 'spring rspec -f doc' do
  # ...
end

NOTE: the above example assumes you have the spring rspec command installed - see here: https://github.com/jonleighton/spring-commands-rspec

Running with bundler

Running bundle exec guard will not run the specs with bundler. You need to change the cmd option to bundle exec rspec:

guard :rspec, cmd: 'bundle exec rspec' do
  # ...
end

List of available options:

cmd: 'zeus rspec'      # Specify a custom rspec command to run, default: 'rspec'
cmd_additional_args: '-f progress' # Any arguments that should be added after the default
                                   # arguments are applied but before the spec list
spec_paths: ['spec']   # Specify a custom array of paths that contain spec files
failed_mode: :focus    # What to do with failed specs
                       # Available values:
                       #  :focus - focus on the first 10 failed specs, rerun till they pass
                       #  :keep - keep failed specs until they pass (add them to new ones)
                       #  :none (default) - just report
all_after_pass: true   # Run all specs after changed specs pass, default: false
all_on_start: true     # Run all the specs at startup, default: false
launchy: nil           # Pass a path to an rspec results file, e.g. ./tmp/spec_results.html
notification: false    # Display notification after the specs are done running, default: true
run_all: { cmd: 'custom rspec command', message: 'custom message' } # Custom options to use when running all specs
title: 'My project'    # Display a custom title for the notification, default: 'RSpec results'
chdir: 'directory'     # run rspec from within a given subdirectory (useful if project has separate specs for submodules)
results_file: 'some/path' # use the given file for storing results (instead of default relative path)
bundler_env: :original_env # Specify which Bundler env to run the cmd under, default: :original_env
                       # Available values:
                       #  :clean_env - old behavior, uses Bundler environment with all bundler-related variables removed. This is deprecated in bundler 1.12.x.
                       #  :original_env (default) - uses Bundler environment present before Bundler was activated
                       #  :inherit - runs inside the current environment

Using Launchy to view rspec results

guard-rspec can be configured to launch a results file in lieu of outputing rspec results to the terminal. Configure your Guardfile with the launchy option:

guard :rspec, cmd: 'rspec -f html -o ./tmp/spec_results.html', launchy: './tmp/spec_results.html' do
  # ...
end

Zeus Integration

You can use plain Zeus or you can use Guard::Zeus for managing the Zeus server (but you'll want to remove the spec watchers from Guard::Zeus, or you'll have tests running multiple times).

Also, if you get warnings about empty environment, be sure to read about this workaround

Using parallel_tests

parallel_tests has a -o option for passing RSpec options, and here's a trick to make it work with Guard::RSpec:

rspec_options = {
  cmd: "bundle exec rspec",
  run_all: {
    cmd: "bundle exec parallel_rspec -o '",
    cmd_additional_args: "'"
  }
}
guard :rspec, rspec_options do
# (...)

(Notice where the ' characters are placed)

Development

Pull requests are very welcome! Please try to follow these simple rules if applicable:

  • Please create a topic branch for every separate change you make.
  • Make sure your patches are well tested. All specs run with rake spec:portability must pass.
  • Update the README.
  • Please do not change the version number.

For questions please join us in our Google group or on #guard (irc.freenode.net).

Author

Thibaud Guillaume-Gentil (@thibaudgg)

Contributors

https://github.com/guard/guard-rspec/contributors