Posted 6 months 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:

Copy
expect { 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.

Copy
expect { 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:

Copy
expect(light.color) .to eq("green") .or eq("yellow") .or eq("red")

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Arne Hartherz
Last edit:
5 months ago
by Thomas Eisenbarth
Keywords:
compound, expectations
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more