Rspec not_to change .and not_to change - how to do it?

Category: Ruby :: Published at: 29.06.2021

We can't chain not_to change .and not_to change in one rspec matcher call.

But the gem creators made for us some simple and very useful workaround:

# we call this line before RSpec.describe. first argument is new matcher call, and second one is the old one we want to negate.
RSpec::Matchers.define_negated_matcher :not_change, :change

# then we can use new matcher like this
expect { accept_agreement }.to not_change(agreement, :street)
  .and not_change(agreement, :apartment_number)
  .and not_change(agreement, :postcode)

And that's all!

- Click if you liked this article

Views: 3400