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
Usually I do:
Where
42
represents the line of the test I want to run.EDIT1:
You could also use tags. See here.
EDIT 2:
Try:
You can pass a regex to the spec command which will only run
it
blocks matching the name you supply.With Rake:
(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.
For model, it will run case on line number 5 only
For controller : it will run case on line number 5 only
For signal model or controller remove line number from above
To run case on all models
To run case on all controller
To run all cases
My preferred method for running specific tests is slightly different - I added the lines
To my spec_helper file.
Now, whenever I want to run one specific test (or context, or spec), I can simply add the tag "focus" to it, and run my test as normal - only the focused test(s) will run. If I remove all the focus tags, the
run_all_when_everything_filtered
kicks in and runs all the tests as normal.It's not quite as quick and easy as the command line options - it does require you to edit the file for the test you want to run. But it gives you a lot more control, I feel.
@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:So instead of doing
rspec spec/unit/baseball_spec.rb:42
where it (test in line 42) is the first test, we can simply dorspec spec/unit/baseball_spec.rb[1:1]
orrspec spec/unit/baseball_spec.rb[1:1:1]
depending on how nested the test case is.