File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
10
10
11
11
For a steady stream of TILs, [ sign up for my newsletter] ( https://crafty-builder-6996.ck.page/e169c61186 ) .
12
12
13
- _ 1435 TILs and counting..._
13
+ _ 1436 TILs and counting..._
14
14
15
15
---
16
16
@@ -1099,6 +1099,7 @@ _1435 TILs and counting..._
1099
1099
1100
1100
### RSpec
1101
1101
1102
+ - [ Avoid Accidentally Disabling Pry] ( rspec/avoid-accidentally-disabling-pry.md )
1102
1103
- [ Check Specific Arguments To Received Method] ( rspec/check-specific-arguments-to-received-method.md )
1103
1104
- [ Find Minimal Set Of Tests Causing A Flicker] ( rspec/find-minimal-set-of-tests-causing-a-flicker.md )
1104
1105
- [ Format Test Results As A JSON File] ( rspec/format-test-results-as-a-json-file.md )
Original file line number Diff line number Diff line change
1
+ # Avoid Accidentally Disabling Pry
2
+
3
+ I was recently working on a test that needed to mock an environment variable in
4
+ order to observe the behavior under test. I initially ended up with the
5
+ following lines in a ` before ` block.
6
+
7
+ ``` ruby
8
+ before do
9
+ allow(ENV ).to receive(:[] ).and_return(" " )
10
+ allow(ENV ).to receive(:[] ).with(" API_SERVER_URL" ).and_return(" localhost" )
11
+ end
12
+ ```
13
+
14
+ The idea was to create a "clean" ` ENV ` for the test and then layer in any
15
+ relevant environment variables from there.
16
+
17
+ This is a misguided approach for a couple reasons. One particular and
18
+ hard-to-debug issue is that this mocking of ` ENV ` accidentally disabled Pry
19
+ (i.e. ` binding.pry ` ). So once I was having issues with my test, I couldn't even
20
+ inspect the code at runtime. It would skip right past any ` binding.pry `
21
+ statement I added.
22
+
23
+ What happened is that [ Pry (specifically ` pry-rails ` ) looks for
24
+ ` ENV['DISABLE_PRY_RAILS'] ` ] ( https://github.com/pry/pry-rails/blob/d8d0c6d87a5b8a3e570e0c80910fb80068f3553c/lib/pry-rails.rb#L6 )
25
+ and if that value is _ truthy_ then it won't require the various ` pry-rails `
26
+ modules.
27
+
28
+ The first ` allow ` has ` ENV ` responding with ` "" ` which is truthy and will
29
+ result in Pry being disabled.
30
+
31
+ A more appropriate default would be ` nil ` . Also consider that there are many
32
+ ways to access ` ENV ` , such as ` #fetch ` .
You can’t perform that action at this time.
0 commit comments