Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a single test in guard for rspec?

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?

like image 559
eirikir Avatar asked Jan 15 '14 01:01

eirikir


People also ask

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

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.


2 Answers

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

like image 133
ChrisBarthol Avatar answered Sep 19 '22 05:09

ChrisBarthol


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"

like image 23
arieljuod Avatar answered Sep 22 '22 05:09

arieljuod