How to run a single RSpec test?

201,295

Solution 1

Not sure how long this has bee available but there is an Rspec configuration for run filtering - so now you can add this to your spec_helper.rb:

RSpec.configure do |config|
  config.filter_run_when_matching :focus
end

And then add a focus tag to the it, context or describe to run only that block:

it 'runs a test', :focus do
  ...test code
end

RSpec documentation:

https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method

Solution 2

Usually I do:

rspec ./spec/controllers/groups_controller_spec.rb:42

Where 42 represents the line of the test I want to run.

You can also use tags. See here.

Using bundle exec:

bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42

Solution 3

With Rake:

rake spec SPEC=path/to/spec.rb

(Credit goes to this answer. Go vote him up.)

EDIT (thanks to @cirosantilli): To run one specific scenario within the spec, you have to supply a regex pattern match that matches the description.

rake spec SPEC=path/to/spec.rb \
          SPEC_OPTS="-e \"should be successful and return 3 items\""

Solution 4

You can pass a regex to the spec command which will only run it blocks matching the name you supply.

spec path/to/my_spec.rb -e "should be the correct answer"

2019 Update: Rspec2 switched from the 'spec' command to the 'rspec' command.

Solution 5

There are many options:

rspec spec                           # All specs
rspec spec/models                    # All specs in the models directory
rspec spec/models/a_model_spec.rb    # All specs in the some_model model spec
rspec spec/models/a_model_spec.rb:nn # Run the spec that includes line 'nn'
rspec -e"text from a test"           # Runs specs that match the text
rspec spec --tag focus               # Runs specs that have :focus => true
rspec spec --tag focus:special       # Run specs that have :focus => special
rspec spec --tag focus ~skip         # Run tests except those with :focus => true
Share:
201,295
AnApprentice
Author by

AnApprentice

working on Matter, a new way to gather professional feedback.

Updated on December 23, 2021

Comments

  • AnApprentice
    AnApprentice over 2 years

    I have the following file:

    /spec/controllers/groups_controller_spec.rb
    

    What command in terminal do I use to run just that spec and in what directory do I run the command?

    My gem file:

    # Test ENVIRONMENT GEMS
    group :development, :test do
        gem "autotest"
        gem "rspec-rails", "~> 2.4"
        gem "cucumber-rails", ">=0.3.2"
        gem "webrat", ">=0.7.2"
        gem 'factory_girl_rails'
        gem 'email_spec'
    end
    

    Spec file:

    require 'spec_helper'
    
    describe GroupsController do
      include Devise::TestHelpers
    
      describe "GET yourgroups" do
        it "should be successful and return 3 items" do
    
          Rails.logger.info 'HAIL MARRY'
    
          get :yourgroups, :format => :json
          response.should be_success
          body = JSON.parse(response.body)
          body.should have(3).items # @user1 has 3 permissions to 3 groups
        end
      end
    end
    
  • AnApprentice
    AnApprentice almost 13 years
    Thanks I tried that but it errors with: $ rake spec spec/controllers/incoming_mails_controller_spec.rb -e "should be successful and return 3 items" rake aborted! (eval):1:in `standard_rake_options': compile error (eval):1: syntax error, unexpected tIDENTIFIER, expecting $end should be successful and return 3 items
  • AnApprentice
    AnApprentice almost 13 years
    Updated with the actual spec file ideas?
  • AnApprentice
    AnApprentice almost 13 years
    Thanks tried that it does not work when I do rake spec /spec/path...:XX I get the error /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/b‌​in/ruby -S bundle exec rspec ./spec/controllers/groups_controller_spec.rb ./spec/controllers/incoming_mails_controller_spec.rb ./spec/lib/mailing_job/mailingjob_find_reply_spec.rb ./spec/models/group_model_spec.rb ./spec/models/user_model_spec.rb
  • AnApprentice
    AnApprentice almost 13 years
    If I try using just RSPEC, i get this error: "$ rspec spec/controllers/groups_controller_spec.rb:19 /Library/Ruby/Gems/1.8/gems/bundler-1.0.0/lib/bundler/runtim‌​e.rb:27:in `setup': You have already activated rspec-core 2.6.2, but your Gemfile requires rspec-core 2.6.0. Consider using bundle exec. (Gem::LoadError) "
  • Douglas F Shearer
    Douglas F Shearer almost 13 years
    If you have a compile error, your spec is not valid ruby. Make sure you are not missing a do after an it, context or describe declaration.
  • muffinista
    muffinista almost 13 years
    You can try "bundle exec rspec spec/controllers/groups_controller_spec.rb:19" in that case
  • muffinista
    muffinista almost 13 years
    It's "spec", not "rake spec".
  • AnApprentice
    AnApprentice almost 13 years
    bundle exec worked but why? Is that a hack any way to avoid that?
  • apneadiving
    apneadiving almost 13 years
    it's not a hack, it makes sure you use the very same version you declared in your gemfile. In your case, the mere rspec failed because the version on your system is more recent than the one in your gemfile.
  • jpw
    jpw over 11 years
    This is the superior answer because it uses the 'rake spec' command not the 'rspec' command. That means the test database is properly re-initialized each time (which doesnt happen if you use 'rspec...')
  • therin
    therin over 10 years
    IMO this solution is dangerous, because it can lead you to think that a test is passing when it actually isn't - for example, if you add a few lines of code ahead of the test you're trying to fix. Tags are a better way to go.
  • apneadiving
    apneadiving almost 10 years
    @therin tags belong to my answer + if you assume you do things wrong, conclusion will always be wrong
  • nnyby
    nnyby almost 10 years
    Is there a way to do this using Django's test framework?
  • styger
    styger over 9 years
    Instead of bundle exec, you can use binstubs
  • Steve
    Steve over 8 years
    If you are testing shared examples from an external file, this method will attempt to run a test at the specified line in the external file as well as the original referenced file.
  • wired00
    wired00 over 8 years
    I definitely prefer this style, because I'm commonly running tests via Rubymine/intelliJ. I like this method also because its similar to using fit/xit in jasmine / with gulp
  • mgold
    mgold over 8 years
    You can use SPEC=path/to/spec.rb:42 to run the test on the given line number, although it seems any it_behaves_like tests will also get run (bug?).
  • Ekkstein
    Ekkstein over 7 years
    I had to add a . (period) to the front of the path to get it to run ---------------------linebreak--------------------- bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
  • Eugen Mayer
    Eugen Mayer over 7 years
    this should be the correct answer, relating to a line number is seriously wrong - any any case
  • bott
    bott over 4 years
    That is amazing! But I think he wanted to run the test not only an example from the test (is a simple test example with only 1 example)
  • Michael Durrant
    Michael Durrant over 4 years
    This requires changing infrastructure. And remembering to change it back. I would recommend not doing this and using just the rspec commands to run the spec with appropriate parameter to indicate which
  • Jason FB
    Jason FB about 4 years
    There are a few reasons this is a bad answer and should not be the accepted answer— while it is often the quickest solution, you are bypassing the rake task and just instructing rspec to run the single test. In the past (prior to Rails 5), this worked fine. Running the rake task is better and less confusing for the dev long-term, the best is with rake spec SPEC=
  • stwr667
    stwr667 over 3 years
    Note that if your shell is ZSH (as is now default with all Macs), you need to wrap the last argument in quotes to prevent the zsh: no matches found error. E.g. rspec "spec/unit/baseball_spec.rb[1:1]"
  • Alien
    Alien over 2 years
    This should be the correct answer, line number changes way too much to be useful.
  • Astm
    Astm about 2 years
    check this bundle exec rspec -f d spec/controllers/groups_controller_spec.rb