Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 1.28 KB

avoid-accidentally-disabling-pry.md

File metadata and controls

32 lines (25 loc) · 1.28 KB

Avoid Accidentally Disabling Pry

I was recently working on a test that needed to mock an environment variable in order to observe the behavior under test. I initially ended up with the following lines in a before block.

before do
  allow(ENV).to receive(:[]).and_return("")
  allow(ENV).to receive(:[]).with("API_SERVER_URL").and_return("localhost")
end

The idea was to create a "clean" ENV for the test and then layer in any relevant environment variables from there.

This is a misguided approach for a couple reasons. One particular and hard-to-debug issue is that this mocking of ENV accidentally disabled Pry (i.e. binding.pry). So once I was having issues with my test, I couldn't even inspect the code at runtime. It would skip right past any binding.pry statement I added.

What happened is that Pry (specifically pry-rails) looks for ENV['DISABLE_PRY_RAILS'] and if that value is truthy then it won't require the various pry-rails modules.

The first allow has ENV responding with "" which is truthy and will result in Pry being disabled.

A more appropriate default would be nil. Also consider that there are many ways to access ENV, such as #fetch.