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

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

Jonathan Tran
1#
Jonathan Tran Published in 2008-09-27 16:11:52Z
 I want to be able to run a single spec file's tests — for the one file I'm editing, for example. rake spec executes all the specs. My project is not a Rails project, so rake spec:doc doesn't work. Don't know if this matters, but here is my directory structure. ./Rakefile ./lib ./lib/cushion.rb ./lib/cushion ./lib/cushion/doc.rb ./lib/cushion/db.rb ./spec ./spec/spec.opts ./spec/spec_helper.rb ./spec/db_spec.rb 
Ray Baxter
2#
Ray Baxter Reply to 2012-05-05 04:13:16Z
 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 
John Bachir
3#
John Bachir Reply to 2012-04-04 21:35:09Z
 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.
Orion Edwards
4#
Orion Edwards Reply to 2008-09-28 20:04:38Z
 If you installed rspec as a plugin rather than as a gem, then you won't have the spec executable. At any rate, All you need to do is run the file using ruby. The rspec code is clever enough to run the tests for you. eg: ruby myclass_spec.rb 
fatgeekuk
5#
 Alternatively, have a look at autotest. Running autotest in a command window will mean that the spec file will be executed whenever you save it. Also, it will be run whenever the file you are speccing is run. For instance, if you have a model spec file called person_spec.rb, and a model file that it is speccing called person.rb, then whenever you save either of these files from your editor, the spec file will be executed.
Zeke
6#
 specky.vim
boblin
7#
 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
Greg Hemphill
8#
Greg Hemphill Reply to 2011-03-01 17:10:31Z
 I was having trouble getting any of these examples to work, maybe because the post is old and the commands have changed? After some poking around I found this works: rspec spec/models/user_spec.rb That will run just the single file and provides useful output in the terminal.
joelparkerhenderson
9#
 Ruby 1.9.2 and Rails 3 have an easy way to run one spec file:  ruby -I spec spec/models/user_spec.rb  Explanation: ruby command tends to be faster than the rake command -I spec means "include the 'spec' directory when looking for files" spec/models/user_spec.rb is the file we want to run.
Michael Durrant
10#
Michael Durrant Reply to 2014-03-17 12:25:39Z
 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.
juanpaco
11#
 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:  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!
Packer
12#
 Lets say, you're running test for creating todo. You can always run that specific todo spec code using the file crete_spec.rb file as below.  rspec/spec/features/controller/spec_file_name.rb Example: Creating rspec spec/features/todos/create_spec.rb Editing rspec spec/features/todos/edit_spec.rb Deleting rspec spec/features/todos/destroy_spec.rb  If you want to run all the specs in one single short.  rspec  If you want to run all the specs in a specific controller user this.  rspec/spec/feaures/controller_name Example: rspec/spec/features/todos  Hope it gives you more understanding!
 Although many great answers were written to this question, none of them uses the Rspec tags approach. I use tags to run one or more specs in different files -- only those related to my current development task. For example, I add the tag "dev" with the value "current": it "creates an user", dev: :current do user = create(:user) expect(user.persisted?).to be_truthy end  then I run bundle exec rspec . --tag dev:current  Different tags/values can be set in individual specs or groups.
 To run all of your rspec files: rspec note: you must be in the root of your project To run one rspec file: rspec 'path_to/spec.rb' note: replace 'path_to/spec.rb' with your path. Quotation marks optional. To run one rspec test from one file: rspec 'path_to/spec.rb:7' note: :7 is the line number where the test starts