Search⌘ K
AI Features

RSpec Predefined Matchers

Explore the key predefined matchers provided by RSpec to write clear and effective tests in Ruby on Rails. Understand how to use matchers like all, change, contain_exactly, satisfy, output, and raise_error, and learn to compose them for complex test conditions.

We'll cover the following...

Built-in RSpec matchers

Before we run the tests, let’s take a quick look at RSpec’s basic matchers. RSpec predefines several matchers. What follows is a list of the most useful ones. We can find them in their official documentation as well.

Some examples are:

Ruby
expect(array).to all(matcher)
expect(actual).to be > expected # (also works with <, >=, <=, and ==)
expect(actual).to be_a(type)
expect(actual).to be_truthy
expect(actual).to be_falsy
expect(actual).to be_nil
expect(actual).to be_between(min, max)
expect(actual).to be_within(delta).of(expected)
expect { block }.to change(receiver, message, &block)
expect(actual).to contain_exactly(expected)
expect(range).to cover(actual_value)
expect(actual).to eq(expected)
expect(actual).to exist
expect(actual).to have_attributes(key/value pairs)
expect(actual).to include(*expected)
expect(actual).to match(regex)
expect { block }.to output(value).to_stdout # also to_stderr
expect { block }.to raise_error(exception)
expect(actual).to satisfy { block }

Most of these mean what they appear to say. A few are ...