ruby - How do you run a single test/spec file in RSpec?

ID : 10086

viewed : 32

Tags : rubytestingrspecruby

Top 5 Answer for ruby - How do you run a single test/spec file in RSpec?

vote vote

91

Or you can skip rake and use the 'rspec' command:

rspec path/to/spec/file.rb 

In your case I think as long as your ./spec/db_spec.rb file includes the appropriate helpers, it should work fine.

If you're using an older version of rspec it is:

spec path/to/spec/file.rb 
vote vote

85

The raw invocation:

 rake spec SPEC=spec/controllers/sessions_controller_spec.rb \           SPEC_OPTS="-e \"should log in with cookie\"" 

Now figure out how to embed this into your editor.

vote vote

77

This question is an old one, but it shows up at the top of Google when searching for how to run a single test. I don't know if it's a recent addition, but to run a single test out of a spec you can do the following:

rspec path/to/spec:<line number> 

where -line number- is a line number that contains part of your test. For example, if you had a spec like:

1:  2: it "should be awesome" do 3:   foo = 3 4:   foo.should eq(3) 5: end 6: 

Let's say it's saved in spec/models/foo_spec.rb. Then you would run:

rspec spec/models/foo_spec.rb:2 

and it would just run that one spec. In fact, that number could be anything from 2 to 5.

Hope this helps!

vote vote

62

You can also use the actual text of the *e*xample test case with -e !

So for:

it "shows the plane arrival time" 

you can use

rspec path/to/spec/file.rb -e 'shows the plane arrival time' ./scripts/spec path/to/spec/file.rb -e 'shows the plane arrival time' 

no need for rake here.

vote vote

58

from help (spec -h):

-l, --line LINE_NUMBER           Execute example group or example at given line.                                  (does not work for dynamically generated examples) 

Example: spec spec/runner_spec.rb -l 162

Top 3 video Explaining ruby - How do you run a single test/spec file in RSpec?

Related QUESTION?