How to run a single RSpec test?

后端 未结 17 672

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:26

    Given you're on a rails 3 project with rspec 2, From the rails root directory:

      bundle exec rspec spec/controllers/groups_controller_spec.rb 
    

    should definitely work. i got tired of typing that so i created an alias to shorten 'bundle exec rspec' to 'bersp'

    'bundle exec' is so that it loads the exact gem environment specified in your gem file: http://gembundler.com/

    Rspec2 switched from the 'spec' command to the 'rspec' command.

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

    You can use

     rspec spec/controllers/groups_controller_spec.rb:<line_number>
    

    line number should be line number of 'describe' or 'it' lines so that it will run tests present in that particular block. instead it will execute all the lines next to line_number.

    also you can create block with custom name and then can execute those blocks only.

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

    I use this guard gem to auto-run my test. It execute test after create or update operations on test file.

    https://github.com/guard/guard-test

    or usually you can run using following command

    rspec spec/controllers/groups_controller_spec.rb

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

    There are many options:

    rspec spec                           # All specs
    rspec spec/models                    # All specs in the models directory
    rspec spec/models/a_model_spec.rb    # All specs in the some_model model spec
    rspec spec/models/a_model_spec.rb:nn # Run the spec that includes line 'nn'
    rspec -e"text from a test"           # Runs specs that match the text
    rspec spec --tag focus               # Runs specs that have :focus => true
    rspec spec --tag focus:special       # Run specs that have :focus => special
    rspec spec --tag focus ~skip         # Run tests except those with :focus => true
    
    0 讨论(0)
  • 2020-12-04 05:32

    Not sure how long this has bee available but there is an Rspec configuration for run filtering - so now you can add this to your spec_helper.rb:

    RSpec.configure do |config|
      config.filter_run_when_matching :focus
    end
    

    And then add a focus tag to the it, context or describe to run only that block:

    it 'runs a test', :focus do
      ...test code
    end
    

    RSpec documentation:

    https://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#filter_run_when_matching-instance_method

    0 讨论(0)
提交回复
热议问题