Easily check for opposite of an existing matcher

Updated . Posted . Visible to the public.

If you want to ensure the opposite of a matcher you already have, you may define a negated matcher. This comes in handy if you want to chain expectations with .and and therefore can not easily use not_to.

spec/support/matchers.rb:

RSpec::Matchers.define_negated_matcher(:keep, :change)

Example usage:

expect { config.switch_quotation! }
  .to change { @other_quotation.reload.last_shown_at.today? }
    .from(false)
    .to(true)
  .and keep { @same_category_quotation.reload.last_shown_at }

(of course you can use this "standalone", too.)

Judith Roth
Last edit
Judith Roth
Posted by Judith Roth to Judith's Dev Notes (2021-05-18 12:22)