Skip to content

Commit e8d554b

Browse files
committed
Add explanation for ExpectChange unsafe autocorrection
1 parent 39a9d89 commit e8d554b

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Master (Unreleased)
44

5+
- Add explanation why `RSpec/ExpectChange` autocorrection is unsafe. ([@lovro-bikic])
56
- Fix false positive in `RSpec/Pending`, where it would mark the default block `it` as an offense. ([@bquorning])
67

78
## 3.5.0 (2025-02-16)

docs/modules/ROOT/pages/cops_rspec.adoc

+11
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,17 @@ or passing a block that reads the attribute value.
21302130
21312131
This cop can be configured using the `EnforcedStyle` option.
21322132
2133+
[#safety-rspecexpectchange]
2134+
=== Safety
2135+
2136+
Autocorrection is unsafe because `block` style evaluates the object
2137+
twice (before and after evaluating `expect` block), whereas
2138+
`method_call` style evaluates the object only once.
2139+
2140+
If you currently pass a block to `change` and expect the object to
2141+
be evaluated before and after `expect` block, changing to
2142+
`method_call` style may break your test.
2143+
21332144
[#examples-rspecexpectchange]
21342145
=== Examples
21352146

lib/rubocop/cop/rspec/expect_change.rb

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ module RSpec
1010
#
1111
# This cop can be configured using the `EnforcedStyle` option.
1212
#
13+
# @safety
14+
# Autocorrection is unsafe because `block` style evaluates the object
15+
# twice (before and after evaluating `expect` block), whereas
16+
# `method_call` style evaluates the object only once.
17+
#
18+
# If you currently pass a block to `change` and expect the object to
19+
# be evaluated before and after `expect` block, changing to
20+
# `method_call` style may break your test.
21+
#
1322
# @example `EnforcedStyle: method_call` (default)
1423
# # bad
1524
# expect { run }.to change { Foo.bar }

0 commit comments

Comments
 (0)