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
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\""
For model, it will run case on line number 5 only
bundle exec rspec spec/models/user_spec.rb:5
For controller : it will run case on line number 5 only
bundle exec rspec spec/controllers/users_controller_spec.rb:5
For signal model or controller remove line number from above
To run case on all models
bundle exec rspec spec/models
To run case on all controller
bundle exec rspec spec/controllers
To run all cases
bundle exec rspec
@apneadiving answer is a neat way of solving this. However, now we have a new method in Rspec 3.3. We can simply run rspec spec/unit/baseball_spec.rb[#context:#it]
instead of using a line number. Taken from here:
RSpec 3.3 introduces a new way to identify examples[...]
For example, this command:
$ rspec spec/unit/baseball_spec.rb[1:2,1:4]
…would run the 2nd and 4th example or group defined under the 1st top-level group defined in spec/unit/baseball_spec.rb.
So instead of doing
rspec spec/unit/baseball_spec.rb:42
where it (test in line 42) is the first test, we can simply do
rspec spec/unit/baseball_spec.rb[1:1]
or rspec spec/unit/baseball_spec.rb[1:1:1]
depending on how nested the test case is.
You can do something like this:
rspec/spec/features/controller/spec_file_name.rb
rspec/spec/features/controller_name.rb #run all the specs in this controller
starting with rspec 2 you can use the following:
# in spec/spec_helper.rb
RSpec.configure do |config|
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
# in spec/any_spec.rb
describe "something" do
it "does something", :focus => true do
# ....
end
end
Run the commands from your project's root directory:
# run all specs in the project's spec folder
bundle exec rspec
# run specs nested under a directory, like controllers
bundle exec rspec spec/controllers
# run a single test file
bundle exec rspec spec/controllers/groups_controller_spec.rb
# run a test or subset of tests within a file
# e.g., if the 'it', 'describe', or 'context' block you wish to test
# starts at line 45, run:
bundle exec rspec spec/controllers/groups_controller_spec.rb:45