RSpec: Is it for `and changes`, for example. `and_not to change`?

I find the .and method very useful for combining many expectations.

 expect { click_button 'Update Boilerplate' @boilerplate_original.reload } .to change { @boilerplate_original.title }.to('A new boilerplate') .and change { @boilerplate_original.intro }.to('Some nice introduction') 

Is there something that allows me to check without changes ?

 .and_not change { @boilerplate_original.intro } 

Something like that? I couldnโ€™t find anything, and itโ€™s hard to find on Google something like โ€œnotโ€.

+7
rspec
source share
2 answers

No, there is no and_not and no general negation operator, as described in https://github.com/rspec/rspec-expectations/issues/493

There is, however, a mechanism for determining the negative version of an existing connector, as described in http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers.define_negated_matcher , which you can use with and .

A complete set of compound matches is documented at https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/compound-expectations

+11
source share

If you are trying to argue that some operation should not change the score, you can do

 expect(something).to change(Foo, :count).by(1).and change(Bar, :count).by(0) 

I don't know if this will work with strings, but maybe you could do something according to:

 intro_before_change = @boilerplate_original.intro expect { click_button 'Update Boilerplate' @boilerplate_original.reload } .to change { @boilerplate_original.title }.to('A new boilerplate') .and change { @boilerplate_original.intro }.to(intro_before_change) 

This is not particularly expressive, but he is doing his job.

+1
source share

All Articles