• 0

How to run only one test in rspec

Mostly while developing its extremely convenient to be able to run just one particular describe block or it block. Rspec lets you do that using a combination of focus: true as well as adding some configuration. Open your spec_helper.rb file and add the following line to your configuration

RSpec.configure do |config|
  .....some stuff....

  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  .....more of some stuff....
end
Once thats done, you can simply use the focus: true parameter in your tests as follows
# To run one describe block
describe MyCustomClass, focus: true do
 ...
end

# To run just one it block
it 'tests something meaningful', focus: true do
  # test something here
end
Now when you run your tests using rails spec, only the one that has focus: true will be executed.
Pro Tip: Just make sure to remove the focus: true before merging your changes upstream to maintain the sanity and friendliness of your teammates.