mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Avoid Accidentally Disabling Pry as an rspec TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1435 TILs and counting..._
|
||||
_1436 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -1099,6 +1099,7 @@ _1435 TILs and counting..._
|
||||
|
||||
### RSpec
|
||||
|
||||
- [Avoid Accidentally Disabling Pry](rspec/avoid-accidentally-disabling-pry.md)
|
||||
- [Check Specific Arguments To Received Method](rspec/check-specific-arguments-to-received-method.md)
|
||||
- [Find Minimal Set Of Tests Causing A Flicker](rspec/find-minimal-set-of-tests-causing-a-flicker.md)
|
||||
- [Format Test Results As A JSON File](rspec/format-test-results-as-a-json-file.md)
|
||||
|
||||
32
rspec/avoid-accidentally-disabling-pry.md
Normal file
32
rspec/avoid-accidentally-disabling-pry.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# 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.
|
||||
|
||||
```ruby
|
||||
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']`](https://github.com/pry/pry-rails/blob/d8d0c6d87a5b8a3e570e0c80910fb80068f3553c/lib/pry-rails.rb#L6)
|
||||
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`.
|
||||
Reference in New Issue
Block a user