[rspec-users] What is the best way to verify that yield is called?
Ben Mabey
ben at benmabey.com
Wed Apr 9 14:01:50 EDT 2008
Rick DeNatale wrote:
> On Wed, Apr 9, 2008 at 11:54 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
>
>
>> What are you looking to specify and how do you envision the syntax?
>>
>> I'm thinking something like:
>>
>> obj.should yield_with(no_args).on(:message)
>>
>> def message
>> yield
>> end
>>
>> list.should yield_with(1).then(2).then(3).on(:each)
>>
>> def each
>> yield 1
>> yield 2
>> yield 3
>> end
>>
Hmmm.. I didn't think about multiple yields...
>> That all make sense?
>>
>
> Sorta, but what about arguments to the message?
>
> def message(x, y, z)
> yield x+y+z
> end
>
> obj.receiving(:message).with(1,2,3).should yield(6)
>
Or maybe:
obj.when_sent(:message, 1,2,3).should yield(6)
I don't know if the extra 'with' layer is needed since the sending of
messages to objects is a very commonly used idiom in ruby.
To keep the rspec syntax consistent your suggestion may be better so I
could see it either way.
> (1..3).receiving(:each_with_index).should
> yield_with([1,0]).then([2,1]).then([3,2])
>
> or
>
> (1..3).receiving(:each_with_index).should yield_with([1,0], [2,1],[3,2])
>
The multiple args in the yield/yield_with call is more consistent with
the way rspec's mocking of return values work (i.e.
mock.shou..and_return([1,2],[0,4]) )
than the 'then' syntax suggested above.
I guess I could go either way on this one as well.
> (1..3).receiving(:inject).with(0).should yield_with ???????
>
> Now it gets tricky since the sequence of yielded values depends on the block .
>
> I don't know that I like where this is going.
>
>
My original goal was only to ease the pain on my eyes for the the simple
cases. I never considered the more involved cases, so you make a good
point. I can't think of an eloquent way of handling this.
I don't think a matcher could ever handle all of the cases.
Hmmm...
-Ben
More information about the rspec-users
mailing list