[rspec-users] Asserting on a yield

Matt Wynne matt at mattwynne.net
Wed Mar 7 20:30:23 UTC 2012


On 7 Mar 2012, at 18:26, Zach Dennis wrote:

> Matt,
> 
> I have typically done what you are already doing, but I am also
> interested in the answer you seek. Another idea might be something
> like since #do-stuff would need to be invoked with a block:
> 
>   expect(thing, :do_stuff).to_yield val

Yeah, I like that.

Is that real yet or is this just an idea?

> 
> Zach
> 
> On Wed, Mar 7, 2012 at 12:56 PM, Matt Wynne <matt at mattwynne.net> wrote:
>> 
>> On 7 Mar 2012, at 11:39, Morten Møller Riis wrote:
>> 
>> On Mar 7, 2012, at 8:22 AM, Matt Wynne wrote:
>> 
>> 
>> Hi all,
>> 
>> I'm spec'ing a method that yields a value. Right now, I spec it like this:
>> 
>>     result = nil
>>     thing.do_stuff { |value| result = value }
>>     result.should == expected
>> 
>> This feels like too much ceremony. What I want to do is something more this:
>> 
>>    thing.do_stuff.should yield_value(expected)
>> 
>> Is there anything built into RSpec to let me do this? If not, how do other
>> people test yields?
>> 
>> cheers,
>> Matt
>> 
>> 
>> How about this?
>> 
>> thing.do_stuff(&:to_s).should == expected
>> 
>> 
>> Yes, that's a neat hack, but I'd prefer to be able to assert on the actual
>> yielded value, instead of the result of calling an arbitrary method on it.
>> 
>> Doesn't anyone else do this?
>> 
>> cheers,
>> Matt
>> 
>> --
>> Freelance programmer & coach
>> Author, http://pragprog.com/book/hwcuc/the-cucumber-book
>> Founder, http://www.relishapp.com/
>> Twitter, https://twitter.com/mattwynne
>> 
>> 
>> 
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> 
> 
> -- 
> 
> --
> @zachdennis
> http://www.continuousthinking.com
> http://www.mutuallyhuman.com
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

--
Freelance programmer & coach
Author, http://pragprog.com/book/hwcuc/the-cucumber-book
Founder, http://www.relishapp.com/
Twitter, https://twitter.com/mattwynne


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120307/22d0cbc2/attachment.html>


More information about the rspec-users mailing list