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

David Chelimsky dchelimsky at gmail.com
Wed Apr 9 11:54:18 EDT 2008


On Wed, Apr 9, 2008 at 10:59 AM, Ben Mabey <ben at benmabey.com> wrote:
>
>  >>
>  >>       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
>  >
>  >
>  >
>  >
>  >
>  >
>  Thanks Rick!
>  Both of these way work but I think they are both too ugly and should be
>  created into a more expressive matcher.

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?

>  Given the above two options can anyone see an advantage to one way or
>  the other?  Or maybe another option is out there that is better?
>
>  Thanks,
>  Ben
>
>
> _______________________________________________
>  rspec-users mailing list
>  rspec-users at rubyforge.org
>  http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list