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, paramsSeeing 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.rbTo 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
endTO 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.rbDebug an issue with multiple rspec tests
rspec './spec/features/my_feature_spec.rb' './spec/controllers/my_controller_spec.rb' --seed=1 --bisectRun all tests with seed values
bundle exec rspec --seed=1 --bisectOther References
- [https://stackoverflow.com/questions/22874385/rspec-controller-test-get-with-multiple-parameters]
- [https://stackoverflow.com/questions/1063073/rspec-controller-testing-blank-response-body]
- [https://stackoverflow.com/questions/9544355/how-do-i-run-only-specific-rspec-tests-in-rails]
- [https://stackoverflow.com/questions/40460249/rails-rspec-run-only-single-test]
- [https://stackoverflow.com/questions/143925/how-do-you-run-a-single-test-spec-file-in-rspec]