How do I `expect` something which raises exception in RSpec?
You can chain positive assertions with and
. If you want to mix in a negated one in the chain, RSpec 3.1 introduced define_negated_matcher
.
You could do something like:
RSpec::Matchers.define_negated_matcher :avoid_changing, :changeexpect { eat(what: nil) } .to raise_error .and avoid_changing(cat, :status)
Inspired by this comment.
You could use the "rescue nil" idiom to shorten what you already have:
it { expect { eat(what: nil) rescue nil }.not_to change(cat, :status) }
In RSpec 3 you can chain the two tests into one. I find this to be more readable than the rescue nil
approach.
it { expect { eat(what: nil) }.to raise_error.and not_to change(cat, :status)}