41

I use guard-rspec to automatically run necessary rspec tests as my files changes, and I love how it works. However, when I'm debugging a file with multiple tests, sometimes I just want an individual test to be re-run. For example, with rspec from the command line:

rspec spec/requests/my_favorite_spec.rb:100

This will run only the single spec at line 100 in my_favorite_spec.rb.

I tried inputting the above into the guard console, but it just ran all the tests as if I had just pressed enter. Is there another syntax in the guard console to run a single spec?

eirikir
  • 3,802
  • 3
  • 21
  • 39

3 Answers3

52

You have to argument your spec/spec_helper.rb file to accept the :focus => true statement.

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

Then you can use

it 'does something', :focus => true do
  //your spec
end

or

describe "something", :focus => true do
  before do
     sign in
     visit page
  end

  it { does something }
  it { does something else }
end

If you are going to take that approach, you probably also want to ensure all specs are run if there is no :focus => true anywhere, using the documented approach:

RSpec.configure do |config|
  config.run_all_when_everything_filtered = true
end

You can do a lot with filters; you might want to have a look at this page: https://relishapp.com/rspec/rspec-core/v/3-0/docs/filtering

Adam Spiers
  • 17,397
  • 5
  • 46
  • 65
ChrisBarthol
  • 4,871
  • 2
  • 21
  • 24
  • This does exactly what I needed, and the link was helpful too. Thanks! – eirikir Jan 16 '14 at 04:27
  • This works, but when I remove `:focus => true` from the `it` block, `guard` says `All examples were filtered out`. Obviously this fails to satisfy the obvious work flow. A full solution would run all examples if there were no `:focus` clauses present. – Adam Spiers Jan 23 '14 at 12:08
  • 3
    OK, it turns out you *can* run all specs when there are no `:focus` clauses, via `config.run_all_when_everything_filtered = true` in the `RSpec.configure` block. Here are [the docs](https://relishapp.com/rspec/rspec-core/v/3-0/docs/configuration/run-all-when-everything-filtered). – Adam Spiers Jan 23 '14 at 12:20
  • Is there not a way to do this from the command line? I'm trying to make command line functionality work for both guard and for parallel specs – Seyon Mar 08 '17 at 02:29
4

I think you can add the "focus: true" option for the spec you want to run, something like

it 'does something', focus: true do
  //your spec
end

then you save the file and guard runs only that focused test

when you are finished you just remove "focus: true"

arieljuod
  • 15,460
  • 2
  • 25
  • 36
4

You can use failed_mode: :focus in your Guardfile, as described here: https://github.com/guard/guard-rspec#list-of-available-options

Rafael Oliveira
  • 2,823
  • 4
  • 33
  • 50