[rspec-users] Asserting on a yield

Matt Wynne matt at mattwynne.net
Wed Mar 7 17:53:41 UTC 2012


On 7 Mar 2012, at 15:12, Ken Chien wrote:

> Hi Matt, 
> On Wed, Mar 7, 2012 at 2:22 AM, Matt Wynne <matt at mattwynne.net> 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?
> 
> 
> I came across this about a month or two ago.  
> You can do this:
>     thing.should_receive(:do_stuff).and_yield( whatever)

Surely that sets up a mocked collaborator to respond by yielding something, doesn't it? That's not what I'm looking for. I want to specify that a method on the class I'm testing yields a certain value.

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/df1922ef/attachment.html>


More information about the rspec-users mailing list