Add new RSpec/ChangeWithoutExpect cop#2074
Conversation
ae49a4e to
07cb1d9
Compare
Follow up: #2071
07cb1d9 to
88d8bf9
Compare
| 'without chains' do | ||
| expect_no_offenses(<<~RUBY) | ||
| it 'changes the count' do | ||
| change(Counter, :count) |
There was a problem hiding this comment.
Why doesn’t it register an offence?
| (send | ||
| { | ||
| (block (send nil? :expect ...) ...) | ||
| (send nil? :expect ...) |
There was a problem hiding this comment.
Why do we match the non-block form here?
|
|
||
| MSG = 'Use `change` matcher within an `expect` block.' | ||
| RESTRICT_ON_SEND = [:change].freeze | ||
| SINGLE_RESTRICTED_METHODS = %i[by by_at_least by_at_most from].freeze |
There was a problem hiding this comment.
| # expect { subject }.to change(Counter, :count).by(1) | ||
| # end | ||
| # | ||
| class ChangeWithoutExpect < RuboCop::Cop::Base |
There was a problem hiding this comment.
Even though this would detect a number of offences, wouldn’t it make more sense to make a more broad cop that would detect all built-in block matchers? “output”, “raise_error”, …
| # expect { subject }.to change(Counter, :count).by(1) | ||
| # end | ||
| # | ||
| class ChangeWithoutExpect < RuboCop::Cop::Base |
| end | ||
| RUBY | ||
| end | ||
| end |
There was a problem hiding this comment.
How does it behave with a case with two statements? Like “subject; change(:foo, :bar)”?
| end | ||
| RUBY | ||
| end | ||
| end |
There was a problem hiding this comment.
What it the matcher is assigned to a variable, and then used multiple times?
“m = change { sum }
expect { foo }.to m
expect { bar }.to m
“
This is a legit RSpec construct, and should work fine?
Follow up: #2071
Before submitting the PR make sure the following are checked:
master(if not - rebase it).CHANGELOG.mdif the new code introduces user-observable changes.bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml.Enabled: pendinginconfig/default.yml.Enabled: truein.rubocop.yml.VersionAdded: "<<next>>"indefault/config.yml.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"inconfig/default.yml.