Fix randomly failing tests easily
Ruby on Rails / RSpec
A randomly failing test is one of the most annoying moments during test creation process. Mostly, such spec doesn’t fail in our development environment but on Continuous Integration service. If you are not familiarized with CI yet then you can check Wikipedia page or services like Travis CI or Circle CI.
Debugging randomly failing specs may be a time-consuming process. The first step is to find the right order of executing specs to reproduce our issue and the second step is to fix the issue. RSpec would not help us with the second step but it can help with finding the right order to reproduce failing scenario. In order to do this, use
bundle exec rspec spec/controllers --bisect
This flag is available from 3.3 version.
Your output should be similar:
Bisect started using options: "spec/controllers" Running suite to find failures... (3.37 seconds) Starting bisect with 2 failing examples and 5 non-failing examples. Checking that failure(s) are order-dependent... failure(s) do not require any non-failures to run first Bisect complete! Reduced necessary non-failing examples from 5 to 0 in 3.25 seconds. The minimal reproduction command is: rspec ./spec/controllers/contacts_controller_spec.rb[1:2:2,1:2:3]
The output will give you an instruction which command you should use to reproduce randomly failing specs. Now it’s your job to fix the issue.