How to run a single RSpec test?
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:
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
AnApprentice
working on Matter, a new way to gather professional feedback.
Updated on December 23, 2021Comments
-
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 almost 13 yearsThanks 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 almost 13 yearsUpdated with the actual spec file ideas?
-
AnApprentice almost 13 yearsThanks 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/bin/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 almost 13 yearsIf 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/runtime.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 almost 13 yearsIf you have a compile error, your spec is not valid ruby. Make sure you are not missing a
do
after anit
,context
ordescribe
declaration. -
muffinista almost 13 yearsYou can try "bundle exec rspec spec/controllers/groups_controller_spec.rb:19" in that case
-
muffinista almost 13 yearsIt's "spec", not "rake spec".
-
AnApprentice almost 13 yearsbundle exec worked but why? Is that a hack any way to avoid that?
-
apneadiving almost 13 yearsit'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 over 11 yearsThis 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 over 10 yearsIMO 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 almost 10 years@therin tags belong to my answer + if you assume you do things wrong, conclusion will always be wrong
-
nnyby almost 10 yearsIs there a way to do this using Django's test framework?
-
styger over 9 yearsInstead of
bundle exec
, you can use binstubs -
Steve over 8 yearsIf 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 over 8 yearsI 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 over 8 yearsYou can use
SPEC=path/to/spec.rb:42
to run the test on the given line number, although it seems anyit_behaves_like
tests will also get run (bug?). -
Ekkstein over 7 yearsI 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 over 7 yearsthis should be the correct answer, relating to a line number is seriously wrong - any any case
-
bott over 4 yearsThat 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 over 4 yearsThis 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 about 4 yearsThere 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 over 3 yearsNote 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 over 2 yearsThis should be the correct answer, line number changes way too much to be useful.
-
Astm about 2 yearscheck this
bundle exec rspec -f d spec/controllers/groups_controller_spec.rb