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

David Chelimsky dchelimsky at gmail.com
Wed Apr 9 12:46:56 EDT 2008


On Wed, Apr 9, 2008 at 12:33 PM, Rick DeNatale <rick.denatale at gmail.com> 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
>  >
>  >  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)
>
>  (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])
>
>  (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.

Agreed, this could get hairy. Perhaps we should chalk this up to
implementation detail?

Looking back at the OP:

it "should yield a message_delivery object" do
  create_message_in_factory do |message_delivery|
    message_delivery.should be_instance_of(MessageDelivery)
  end
end

How about something more like this as an idiom:

it "should yield a message_delivery object" do
  create_message_in_factory do |message_delivery|
    return message_delivery
  end.should be_instance_of(MessageDelivery)
end

This still uses the contents of the blog to set an expectation, but is
perhaps more expressive about the fact that we're not really
interested in the contents of the block as much as we are what the end
result is.

WDYT?


More information about the rspec-users mailing list