How to run a single RSpec test?

后端 未结 17 671

I have the following file:

/spec/controllers/groups_controller_spec.rb

What command in terminal do I use to run just that spec and in what

相关标签:
17条回答
  • 2020-12-04 05:06

    My preferred method for running specific tests is slightly different - I added the lines

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

    To my spec_helper file.

    Now, whenever I want to run one specific test (or context, or spec), I can simply add the tag "focus" to it, and run my test as normal - only the focused test(s) will run. If I remove all the focus tags, the run_all_when_everything_filtered kicks in and runs all the tests as normal.

    It's not quite as quick and easy as the command line options - it does require you to edit the file for the test you want to run. But it gives you a lot more control, I feel.

    0 讨论(0)
  • 2020-12-04 05:06

    In rails 5,

    I used this way to run single test file(all the tests in one file)

    rails test -n /TopicsControllerTest/ -v
    

    Class name can be used to match to the desired file TopicsControllerTest

    My class class TopicsControllerTest < ActionDispatch::IntegrationTest

    Output :

    If You want you can tweak the regex to match to single test method \TopicsControllerTest#test_Should_delete\

    rails test -n /TopicsControllerTest#test_Should_delete/ -v
    
    0 讨论(0)
  • 2020-12-04 05:09

    For single example of spec file you need to add line number at the last , For Example

    rspec spec/controllers/api/v1/card_list_controller_spec.rb:35
    

    For single file you can specify your file path, For Example

    rspec spec/controllers/api/v1/card_list_controller_spec.rb
    

    For Whole Rspec Example in spec folder, you can try with this command

    bundle exec rspec spec
    
    0 讨论(0)
  • 2020-12-04 05:11

    You can pass a regex to the spec command which will only run it blocks matching the name you supply.

    spec path/to/my_spec.rb -e "should be the correct answer"
    

    2019 Update: Rspec2 switched from the 'spec' command to the 'rspec' command.

    0 讨论(0)
  • 2020-12-04 05:16

    Another common mistake is to still have or have upgraded an older Rails app to Rails 5+ and be putting require 'spec_helper' at the top of each test file. This should changed to require 'rails_helper'. If you are seeing different behavior between the rake task (rake spec) and when you run a single spec (rspec path/to/spec.rb), this is a common reason

    the best solution is to

    1) make sure you are using require 'rails_helper' at the top of each of your spec files — not the older-style require 'spec_helper' 2) use the rake spec SPEC=path/to/spec.rb syntax

    the older-style rspec path/to/spec.rb I think should be considered out-of-vogue by the community at this time in 2020 (but of course you will get it to work, other considerations aside)

    0 讨论(0)
  • 2020-12-04 05:17

    Usually I do:

    rspec ./spec/controllers/groups_controller_spec.rb:42
    

    Where 42 represents the line of the test I want to run.

    EDIT1:

    You could also use tags. See here.

    EDIT 2:

    Try:

    bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
    
    0 讨论(0)
提交回复
热议问题