Parts of an Expectation

When you see an expectation like the following one:

 expect​(deck.cards.count).to eq(52), ​'not playing with a full deck'

…you’ll notice several forms of punctuation in use: parentheses, dots, and whitespace. While there’s some variety here, the syntax consistently uses just a few simple parts:

  • A subject—the thing you’re testing—that is, an instance of a Ruby class

  • A matcher—an object that specifies what you expect to be true about the subject, and provides the pass/fail logic

  • (optionally) A custom failure message

These parts are held together with a bit of glue code: expect, together with either the to or not_to method.

Let’s get a feel for how these parts ...

Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.