Using Pry to Troubleshoot Test Failures
Learn the use of Pry to troubleshoot test failures by using examples.
Failures troubleshooting with Pry
Pry is really a wonderful console, but again, this is Rails Test Prescriptions, not Rails Console Prescriptions. Luckily, we can invoke Pry directly from inside a test.
Pry session
If we add the line binding.pry
to our code, then a Pry session will start when the code execution reaches that line.
Let’s see how this works. We’ll put a binding.pry
in one of the tests in the action test file creates_project_spec.rb
. Can you guess which line from this session trace contains binding.pry
?
Pry not only opens a session, but it also displays the test where it stopped, and it sets the Pry scope to the test class (again, this is syntax-highlighted).
We can use the cd
notation to see what’s going on:
[1] pry(#<RSpec::ExampleGroups::CreatesProject::Initialization>)> cd creator[2] pry(#<CreatesProject>):1> ls[3] pry(#<CreatesProject>):1> cd project[4] pry(#<Project>):2> name
And then, if we use “Ctrl+C” to exit Pry, the tests ...