1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-14 20:48:01 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
nick-w-nick
b7a405a4fe Merge 295fe153ad into 4ff1a381d1 2024-08-14 11:25:22 -04:00
jbranchaud
4ff1a381d1 Add Avoid Accidentally Disabling Pry as an rspec TIL 2024-08-14 10:09:07 -05:00
nick-w-nick
295fe153ad added mention of ES6 compatibility
Hello, I've added a small blockquote below the description to indicate that this method of accessing an indefinite number of function arguments has been superseded by the use of the spread operator via rest parameters for ES6+ compatibility.
2022-01-06 11:39:04 -05:00
3 changed files with 36 additions and 1 deletions

View File

@@ -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)

View File

@@ -5,6 +5,8 @@ an array-like object with all of the arguments to the function. Even if not
all of the arguments are referenced in the function signature, they can
still be accessed via the `arguments` object.
> For ES6+ compatibility, the `spread` operator used via [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) is preferred over the `arugments` object when accessing an abritrary number of function arguments.
```javascript
function argTest(one) {
console.log(one);

View 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`.