Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect multiple not_to change expectations in rspec

I'm trying to make sure certain data has remain unchanged by a single action:

expect {
  # running migration and user reload here
}.not_to change(user, :avatar_url).from(sample_avatar_url).and change(user, :old_avatar).from(nil)

sample_avatar_url is a string defined in the beginning of the spec file.

Basically, I want to check whether the avatar_url and old_avatar remain untouched by what's happening in the expect block.

The output from the above code is:

expect(...).not_to matcher.and matcher is not supported, since it creates a bit of an ambiguity. Instead, define negated versions of whatever matchers you wish to negate with RSpec::Matchers.define_negated_matcher and use expect(...).to matcher.and matcher.

like image 374
abpetkov Avatar asked Apr 19 '16 16:04

abpetkov


1 Answers

This doesn't work because it's not clear reading whether thats supposed to mean not change the first and not change the second, or not change the first but change the second. You have a couple of options to get around this

Since you're checking static values just don't use change

..run migration and user reload..
expect(user.avatar_url).to eq(sample_avatar_url)
expect(user.old_avatar).to eq nil

or use define_negated_matcher to create a not_change matcher

RSpec::Matchers.define_negated_matcher :not_change, :change
expect {
  # running migration and user reload here
}.to not_change(user, :avatar_url).from(sample_avatar_url).and not_change(user, :old_avatar).from(nil)
like image 158
Thomas Walpole Avatar answered Oct 14 '22 07:10

Thomas Walpole