ruby-on-rails - tests - rspec specific line

How to run a single RSpec test? (8)

In rails 5,

I used this way to run single test file(all the tests in one file)

rails test -n /TopicsControllerTest/ -v

Class name can be used to match to the desired file TopicsControllerTest

My class class TopicsControllerTest < ActionDispatch::IntegrationTest

Output :

If You want you can tweak the regex to match to single test method \TopicsControllerTest#test_Should_delete\

rails test -n /TopicsControllerTest#test_Should_delete/ -v

I have the following file:


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

My gem file:

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'

Spec file:

require 'spec_helper'

describe GroupsController do
  include Devise::TestHelpers

  describe "GET yourgroups" do
    it "should be successful and return 3 items" do '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

@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.

Given you're on a rails 3 project with rspec 2, From the rails root directory:

  bundle exec rspec spec/controllers/groups_controller_spec.rb 

should definitely work. i got tired of typing that so i created an alias to shorten 'bundle exec rspec' to 'bersp'

'bundle exec' is so that it loads the exact gem environment specified in your gem file:

Rspec2 switched from the 'spec' command to the 'rspec' command.

I use this guard gem to auto-run my test. It execute test after create or update operations on test file.

or usually you can run using following command

rspec spec/controllers/groups_controller_spec.rb

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

Usually I do:

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

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


You could also use tags. See here.



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

You can do something like this:

 rspec/spec/features/controller_name.rb         #run all the specs in this controller

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"