O'Reilly logo

Rails 4 Test Prescriptions by Noel Rappin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Write Your Own RSpec Matchers

RSpec’s built-in matchers are flexible, but sometimes you have behavior patterns you specify multiple times in your code and the existing matchers don’t quite cover it. Sometimes this is because the specification requires multiple steps, and sometimes it’s because the generic matcher doesn’t quite match the code’s intent.

RSpec provides tools for creating your own custom matchers to cover just such an eventuality. A basic matcher is really simple. Let’s say we wanted a custom matcher to measure project size in points. Rather than say expect(project.size).to eq(5) we’d say expect(project).to be_of_size(5). It’s a little contrived, but work with me.

Normally custom matchers are placed in the spec/support folder, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required