Posted over 3 years ago. Visible to the public. Repeats. Linked content.
RSpec 3 allows chaining multiple expectations
When you are using lambdas in RSpec to assert certain changes of a call, you know this syntax:
Copyexpect { playlist.destroy }.to change { Playlist.count }.by(-1)
While you can define multiple assertions through multiple specs, you may not want to do so, e.g. for performance or for the sake of mental overhead.
RSpec allows chaining expectations simply by using and
.
Copyexpect { playlist.destroy } .to change { Playlist.count }.by(-1) .and change { Video.count }.by(0)
The above example will call playlist.destroy
only once, but test both assertions.
In addition to and
, RSpec also offers or
which means that only one condition needs to be satisfied. Example from the docs:
Copyexpect(light.color) .to eq("green") .or eq("yellow") .or eq("red")
See also
makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.