[rspec-users] How to write advanced matchers

Erik Pukinskis erik at snowedin.net
Tue Nov 18 23:06:22 EST 2008

Hi Folks!

For any developers reading... thank you for RSpec... it's wonderful!
I've been trying my darndest to get into it, spec everything ahead of
time, and it's working out great.

The one thing I really have a hard time with though is writing more
advanced custom matchers.  I can write simple ones, but the code for
the fancier matchers in RSpec is much more opaque.  Often the code for
something like an_instance_of is shared across several matchers, or
it's being done partly by the translator?  It's hard to tell.  And so
it's hard to write a custom matcher that does the same kind of job.

I was wondering if someone with better Ruby skillz than I have, and
more RSpec knowledge might explain how to structure some of the
slightly fancier types of matchers.  Like how do you write...

* a matcher that accepts a block and passes on some information, like has_tag?
* a matcher that can go in that block and use that information, like with_tag?
* a mock argument matcher like is_instance_of or hash_including?

It'd be wonderful if someone could post simple examples of how you'd
write custom matchers of those types.  I've tried
(http://erikonrails.snowedin.net/?p=33) and failed.

All the best,

Erik Pukinskis

More information about the rspec-users mailing list