[rspec-users] Custom matcher for predicates

Ed Howland ed.howland at gmail.com
Sat Dec 26 18:53:19 EST 2009

I hope this isn't a dumb question, but can a custom matcher be written
for a possibly non-existant predicate? I know that if the object
responds to some predicate? message, RSpec will breate a custom
matcher on the fly for it. Such as be_naughty or be_nice for
sarah.naughty? and jane.nice?

But what if you want to create your own where this is not the case.
Like sarah.should_not be_on_santas_list:

Spec::Matchers.define :be_on_santas_list do |expected|
  matcher do |actual|
    $santas_list.include? actual

Or in the situation where the object has a predicate that returns a
string and not true or false. As is the case with

  match do |actual|
    actual.stand_alone? == 'yes'

This works, but the value of expected is nil.

Is this Ok? How do others handle this? .

Thanks, and Happy Holidays.

Ed Howland

More information about the rspec-users mailing list