Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec: Is there a not for `and change`, e.g. `and_not to change`?

Tags:

rspec

I find the .and method very useful for chaining 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 let's me check for no change?

.and_not change { @boilerplate_original.intro }

Something like that? I couldn't find anything, and it's hard to search on Google for something like "and not".

like image 633
Joshua Muheim Avatar asked Jan 23 '16 18:01

Joshua Muheim


2 Answers

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

There is, however, a mechanism to define a negated version of an existing matcher, as described in http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers.define_negated_matcher, which you can use with and.

The full set of compound matchers is documented at https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/compound-expectations

like image 63
Peter Alfvin Avatar answered Oct 11 '22 11:10

Peter Alfvin


If you are trying to assert that some operation should not change a count, you could do

expect { something }.to change { Foo.count }.by(1).and change { Bar.count }.by(0)
like image 42
Alexander Popov Avatar answered Oct 11 '22 12:10

Alexander Popov