Skip to content

Commit 40b302b

Browse files
committed
Improved documentation for RSpec/ChangeByZero
This PR is improves documentation for `RSpec/ChangeByZero`. Since it isn’t mentioned that we define our own negation matcher for `not_change` for the compound expectations case, I felt it would be a bit confusing, so I'll explicitly mention that define a negation matcher for the good case. ```ruby define_negated_matcher :not_change, :change expect { run } .to not_change(Foo, :bar) .and not_change(Foo, :baz) expect { run } .to not_change { Foo.bar } .and not_change { Foo.baz } ```
1 parent bb046eb commit 40b302b

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ Prefer negated matchers over `to change.by(0)`.
404404
# bad
405405
expect { run }.to change(Foo, :bar).by(0)
406406
expect { run }.to change { Foo.bar }.by(0)
407+
408+
# bad - compound expectations
407409
expect { run }
408410
.to change(Foo, :bar).by(0)
409411
.and change(Foo, :baz).by(0)
@@ -414,6 +416,9 @@ expect { run }
414416
# good
415417
expect { run }.not_to change(Foo, :bar)
416418
expect { run }.not_to change { Foo.bar }
419+
420+
# good - compound expectations
421+
define_negated_matcher :not_change, :change
417422
expect { run }
418423
.to not_change(Foo, :bar)
419424
.and not_change(Foo, :baz)

lib/rubocop/cop/rspec/change_by_zero.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module RSpec
99
# # bad
1010
# expect { run }.to change(Foo, :bar).by(0)
1111
# expect { run }.to change { Foo.bar }.by(0)
12+
#
13+
# # bad - compound expectations
1214
# expect { run }
1315
# .to change(Foo, :bar).by(0)
1416
# .and change(Foo, :baz).by(0)
@@ -19,6 +21,9 @@ module RSpec
1921
# # good
2022
# expect { run }.not_to change(Foo, :bar)
2123
# expect { run }.not_to change { Foo.bar }
24+
#
25+
# # good - compound expectations
26+
# define_negated_matcher :not_change, :change
2227
# expect { run }
2328
# .to not_change(Foo, :bar)
2429
# .and not_change(Foo, :baz)

0 commit comments

Comments
 (0)