Home rspec how to run a single test?
Reply: 13

rspec how to run a single test?

AnApprentice
1#
AnApprentice Published in 2011-05-24 20:45:47Z

I have found all kinds of links online but none that are current and show how to run a single test.

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 dir 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
Brendon Muir
2#
Brendon Muir Reply to 2016-11-17 23:59:49Z

Usually I do:

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

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

EDIT1:

You could also use tags. See here.

EDIT 2:

Try:

bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
rogerdpack
3#
rogerdpack Reply to 2015-12-15 22:43:36Z

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"
MissingHandle
4#
MissingHandle Reply to 2011-05-24 20:58:42Z

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: http://gembundler.com/

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

GlyphGryph
5#
GlyphGryph Reply to 2011-10-21 17:47:48Z

My preferred method for running specific tests is slightly different - I added the lines

  RSpec.configure do |config|
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true
  end

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.

Community
6#
Community Reply to 2017-05-23 11:54:50Z

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\""
Rameshwar Vyevhare
7#
Rameshwar Vyevhare Reply to 2013-12-02 12:15:44Z

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

https://github.com/guard/guard-test

or usually you can run using following command

rspec spec/controllers/groups_controller_spec.rb

Michael Durrant
8#
Michael Durrant Reply to 2015-05-14 00:17:14Z

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
Packer
9#
Packer Reply to 2014-12-08 10:50:26Z

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
Ingo
10#
Ingo Reply to 2015-06-16 15:17:40Z

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

Rudi
11#
Rudi Reply to 2016-11-04 19:54:02Z

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
Alupotha
12#
Alupotha Reply to 2016-12-16 11:46:04Z

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
Sandeep Kapil
13#
Sandeep Kapil Reply to 2016-12-21 11:00:58Z

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 
Mohamed Sami
14#
Mohamed Sami Reply to 2017-01-24 21:25:07Z

There is a gem 'watchr' which can track any code change in the file and run the test automatically for you

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304495 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO