rubocop / rubocop-rspec

Code style checking for RSpec files.
https://docs.rubocop.org/rubocop-rspec
MIT License
810 stars 276 forks source link

Suggestion: ExpectChange cop applied on negated matcher #813

Open Drowze opened 5 years ago

Drowze commented 5 years ago

On RSpec one can do:

expect { ... }.to change { ... }
              .and change { ... }

But it is not possible to do

expect { ... }.not_to change { ... }
              .and_not change { ... }

A common solution for this is defining a negated matcher for change:

RSpec::Matchers.define_negated_matcher :not_change, :change

...
expect { ... }.to not_change { ... }
              .and not_change { ... }

On doing this however, we lose the ExpectChange cop usefulness. I thought of creating a custom cop for it for my needs, as not_change is not standard. However, being it a common practice, I think that it would be nice for the ExpectChange cop to have an option where you'd explicit the negated matcher method.

e.g.:

RSpec/ExpectChange:
  NegatedMatcher: not_change

What do you think?

pirj commented 5 years ago

Seems closely related to https://github.com/rubocop-hq/rubocop-rspec/issues/333