Skip to content

Instantly share code, notes, and snippets.

@rrcobb
Created January 23, 2018 00:09
Show Gist options
  • Save rrcobb/8f12d51e2d1628068d2f51ce398c1d7a to your computer and use it in GitHub Desktop.
Save rrcobb/8f12d51e2d1628068d2f51ce398c1d7a to your computer and use it in GitHub Desktop.
Rspec negated change matcher
# This should be in rspec core, but add it here so we can chain negative change expectations, like
# expect { do_thing }.to change {a}.and not_change {b}.and change {b}
RSpec::Matchers.define_negated_matcher :not_change, :change
@rrcobb
Copy link
Author

rrcobb commented Jan 23, 2018

When expecting multiple things to happen given some code block, sometimes you want some things not to change. This is annoying with the default matchers in rspec, so define this in an rspec initializer.

expect { something }
    .to change { a }.from(this).to(that)
    .and not_change { b }
    .and change { c } # etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment