Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run only specific tests in Rspec?

Tags:

ruby

rspec

People also ask

How do I run a RSpec test in terminal?

Open your terminal, cd into the project directory, and run rspec spec . The spec is the folder in which rspec will find the tests. You should see output saying something about “uninitialized constant Object::Book”; this just means there's no Book class.

How do I run an RSpec test in Rubymine?

Create a test configuration from the editorSelect Create 'RSpec: <test name>' or Create 'Minitest: <test name>' and press Enter . In the dialog that opens, specify the run/debug configuration parameters (RSpec or Minitest), apply changes and close the dialog.

How do I use RSpec gems?

Boot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available. That's it.


It isn't easy to find the documentation, but you can tag examples with a hash. Eg.

# spec/my_spec.rb
describe SomeContext do
  it "won't run this" do
    raise "never reached"
  end

  it "will run this", :focus => true do
    1.should == 1
  end
end

$ rspec --tag focus spec/my_spec.rb

More info on GitHub. (anyone with a better link, please advise)

(update)

RSpec is now superbly documented here. See the --tag option section for details.

As of v2.6 this kind of tag can be expressed even more simply by including the configuration option treat_symbols_as_metadata_keys_with_true_values, which allows you to do:

describe "Awesome feature", :awesome do

where :awesome is treated as if it were :awesome => true.

Also see this answer for how to configure RSpec to automatically run 'focused' tests. This works especially well with Guard.


You can run all tests that contain a specific string with --example (or -e) option:

rspec spec/models/user_spec.rb -e "User is admin"

I use that one the most.


Make sure RSpec is configured in your spec_helper.rb to pay attention to focus:

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

Then in your specs, add focus: true as an argument:

it 'can do so and so', focus: true do
  # This is the only test that will run
end

You can also focus tests by changing it to fit (or exclude tests with xit), like so:

fit 'can do so and so' do
  # This is the only test that will run
end

alternatively you can pass the line number: rspec spec/my_spec.rb:75 - the line number can point to a single spec or a context/describe block (running all specs in that block)