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

Ben Mabey ben at benmabey.com
Wed Apr 9 01:29:50 EDT 2008

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)

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
        @block_yieled.should be_true

Seems kinda hackish, but it did work.  Is this the best way to spec this 
or is there already a matcher for this?

If there isn't currently a matcher would be the above way be a good way 
to create a custom matcher?

Thanks for your thoughts,


More information about the rspec-users mailing list