File tree 2 files changed +44
-4
lines changed
2 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -2125,11 +2125,32 @@ expect(false).to eq(true)
2125
2125
2126
2126
Checks for consistent style of change matcher.
2127
2127
2128
- Enforces either passing object and attribute as arguments to the matcher
2129
- or passing a block that reads the attribute value .
2128
+ Enforces either passing a receiver and message as method arguments,
2129
+ or a block.
2130
2130
2131
2131
This cop can be configured using the `EnforcedStyle` option.
2132
2132
2133
+ [#safety-rspecexpectchange]
2134
+ === Safety
2135
+
2136
+ Autocorrection is unsafe because `method_call` style calls the
2137
+ receiver *once* and sends the message to it before and after
2138
+ calling the `expect` block, whereas `block` style calls the
2139
+ expression *twice*, including the receiver.
2140
+
2141
+ If your receiver is dynamic (e.g., the result of a method call) and
2142
+ you expect it to be called before and after the `expect` block,
2143
+ changing from `block` to `method_call` style may break your test.
2144
+
2145
+ [source,ruby]
2146
+ ----
2147
+ expect { run }.to change { my_method.message }
2148
+ # `my_method` is called before and after `run`
2149
+
2150
+ expect { run }.to change(my_method, :message)
2151
+ # `my_method` is called once, but `message` is called on it twice
2152
+ ----
2153
+
2133
2154
[#examples-rspecexpectchange]
2134
2155
=== Examples
2135
2156
Original file line number Diff line number Diff line change @@ -5,11 +5,30 @@ module Cop
5
5
module RSpec
6
6
# Checks for consistent style of change matcher.
7
7
#
8
- # Enforces either passing object and attribute as arguments to the matcher
9
- # or passing a block that reads the attribute value .
8
+ # Enforces either passing a receiver and message as method arguments,
9
+ # or a block.
10
10
#
11
11
# This cop can be configured using the `EnforcedStyle` option.
12
12
#
13
+ # @safety
14
+ # Autocorrection is unsafe because `method_call` style calls the
15
+ # receiver *once* and sends the message to it before and after
16
+ # calling the `expect` block, whereas `block` style calls the
17
+ # expression *twice*, including the receiver.
18
+ #
19
+ # If your receiver is dynamic (e.g., the result of a method call) and
20
+ # you expect it to be called before and after the `expect` block,
21
+ # changing from `block` to `method_call` style may break your test.
22
+ #
23
+ # [source,ruby]
24
+ # ----
25
+ # expect { run }.to change { my_method.message }
26
+ # # `my_method` is called before and after `run`
27
+ #
28
+ # expect { run }.to change(my_method, :message)
29
+ # # `my_method` is called once, but `message` is called on it twice
30
+ # ----
31
+ #
13
32
# @example `EnforcedStyle: method_call` (default)
14
33
# # bad
15
34
# expect { run }.to change { Foo.bar }
You can’t perform that action at this time.
0 commit comments