O'Reilly logo

Effective Testing with RSpec 3 by Myron Marston, Ian Dees

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

We cover:

  • How good matchers make for readable specs and helpful output
  • How to define new matchers in terms of RSpec’s built-in ones
  • How to use RSpec’s DSL to make a new matcher
  • What a custom matcher class written by hand looks like

Chapter 12Creating Custom Matchers

In the previous chapter, we took a tour of the matchers that ship with RSpec. You can be productive in RSpec with just these matchers. On simpler projects, they’re all you’ll need.

Eventually, though, you’re going to hit the limits of the built-in matchers. Because they’re meant for testing general-purpose Ruby code, they require you to speak in Ruby terms rather than your project’s terms.

For example, the following expectations are a jumble of Ruby method calls and hard-coded values: ...

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