develop with

Rspec tips and tricks

How to run multiple and individual tests. How to add details to the tests.

General Tips

The negative of .to is .not_to.

##Passing in params to a controller method##

params = {:id=>"1445", :project_id=>1144, :accesskey=>"MyString", format: :json}
get :show, params

Seeing a view rendered

describe MyController do
  render_views
    describe "GET index " do
   
    it "success" do
      get 'show'
      expect(response).to be_success
      expect(response.status).to eq(200)
      expect(response.body).to include("Hello World")
    end
  
  end

end

##Running tests##

Run individual rspec ###

To run all tests on a single rspec file, run the following:

bundle exec rspec spec/controllers/my_controller_spec.rb

To run a single test within that rspec file, there are a couple options. You can pass in the line number using flag -l, example -l 12.

You can also use regex with the -e flag. For example:

bundle exec rspec spec/controllers/my_controller_spec.rb -e "/my test/"

Tagging your tests will help create a suite of tests that can be run for a prerun or a more thorough run in CI. This is helpful when you have a pull request based CI process and a nightly job running. In order to do this specify tagname: true on your it definition.

For example:

describe MyController do
  it "longer test to run" do
    raise "never reached"
  end

  it "shorter test to run", shortrun: true do
    1.should == 1
  end
end

TO run all tests with a give tag, run the following with your tag name (eg. shortrun):

bundle exec rspec --tag shortrun 

This can be used in conjunction with specifying a specific rspec file. Which means just run the tests with the given tag on that specific test file.

bundle exec rspec --tag shortrun spec/controllers/my_controller_spec.rb

Debug an issue with multiple rspec tests

rspec './spec/features/my_feature_spec.rb' './spec/controllers/my_controller_spec.rb' --seed=1 --bisect

Run all tests with seed values

bundle exec rspec --seed=1 --bisect

Other References ##

comments powered by Disqus

Want to see a topic covered? create a suggestion

Get more developer references and books in the developwith store.