Forum: RSpec How to write advanced matchers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6015f1c7d2c5057f668f4951f3303b28?d=identicon&s=25 Erik Pukinskis (Guest)
on 2008-11-19 05:06
(Received via mailing list)
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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-11-24 16:43
(Received via mailing list)
"Erik Pukinskis" <erik@snowedin.net> writes:

> * 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 hope this doesn't come across as rude...but considering that RSpec is
open source, you just listed a bunch of examples for yourself to check
out!  Now all you have to do is look at the source for them.

Pat
6015f1c7d2c5057f668f4951f3303b28?d=identicon&s=25 Erik Pukinskis (Guest)
on 2008-11-25 01:37
(Received via mailing list)
Hey Pat,

Thanks for your response!  Even if it's just to say RTFC.  :)  I have
been digging into the source regularly.  It's a bit too complicated
for me to understand right now, and I find often the way core matchers
are written is appropriate for code that is going to be distributed
with RSpec, but not for code that is going to be maintained outside of
RSpec.

But you're right, once I understand the RSpec internals, It'll be easy
to write matchers with a different architecture.  It's just slow going
sometimes.

best,

Erik
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-25 04:44
(Received via mailing list)
On Mon, Nov 24, 2008 at 6:36 PM, Erik Pukinskis <erik@snowedin.net>
wrote:
>> I hope this doesn't come across as rude...but considering that RSpec is
>> open source, you just listed a bunch of examples for yourself to check
>> out!  Now all you have to do is look at the source for them.
>>
>> Pat

> Hey Pat,
>
> Thanks for your response!  Even if it's just to say RTFC.  :)  I have
> been digging into the source regularly.  It's a bit too complicated
> for me to understand right now, and I find often the way core matchers
> are written is appropriate for code that is going to be distributed
> with RSpec, but not for code that is going to be maintained outside of
> RSpec.

Would you mind elaborating on this? What about the way it is written
makes it appropriate to ship w/ RSpec but inappropriate for your own
code?

Cheers,
David
This topic is locked and can not be replied to.