Combine Pry and RSpec
RSpec provides a way for you to register aliases for the methods you use to create your specs. You can alias these methods and tie them into predefined hooks using metadata.
For example, let’s say we wanted to have run pry whenever a spec includes some metadata like: pry: true
. Instead of adding pry: true
after each spec, we can add an alias by doing the following:
RSpec.configure do |config|
config.alias_example_group_to :pdescribe, pry: true
config.alias_example_to :pit, pry: true
config.after(:example, pry: true) do |e|
require 'pry'
binding.pry
end
end
We’ve registered 2 aliases. Whenever we prepend p
onto a method’s name, we’ll now get the same functionality as adding pry: true
to our spec’s metadata.
We’ve tied this alias into an after
hook. This hook only runs when pry is set to true in the spec’s metadata. RSpec after hooks always run, even if the spec fails!
Simply prepend p
to a spec and pry will run once the spec is finished!
We’ve now created a powerful pry alias to debug our specs in RSpec :)
Tweet