[rspec-users] What is the best way to verify that yield is called?

Rick DeNatale rick.denatale at gmail.com
Wed Apr 9 08:50:13 EDT 2008


On Wed, Apr 9, 2008 at 1:29 AM, Ben Mabey <ben at benmabey.com> wrote:
> Hello all,
>  What is the best way to verify that a method yields a block that is
>  passed in?
>
>  Before I added any 'yield' to my method I created this spec:
>
>       it "should yield a message_delivery object" do
>         create_message_in_factory do |message_delivery|
>           message_delivery.should be_instance_of(MessageDelivery)
>         end
>       end
>
>  This passed without me modifying my method which is not what I was
>  hoping for.  So I tried this spec and it gave me red:
>
>       it "should yield the given block" do
>         @block_yieled = false
>         create_message_in_factory do |message_delivery|
>           @block_yieled = true
>         end
>         @block_yieled.should be_true
>       end
>
>
>  Seems kinda hackish, but it did work.  Is this the best way to spec this
>  or is there already a matcher for this?

Another approach might be:

    it "should yield the given block" do
        block_body = mock("block body")
        block_body.should_receive(:got_here)
        create_message_in_factory_do | message_delivery |
            block_body.got_here
        end
   end





-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/


More information about the rspec-users mailing list