Forum: Ruby Verifying that RSpec mock object methods are called with a b

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6ce13001d5cc7bedfd33810695be6f20?d=identicon&s=25 Peter Havens (tekhne)
on 2007-04-17 00:25
(Received via mailing list)
In RSpec, is there a way to verify that a mock object method was
called with a block? For example, something like this:

context "A mock object method" do
  specify "should verify that it was called with a block" do
    m = mock( "mock object" )
    m.should_receive( :test_method ).with( :block )
    m.test_method { :test_result }
  end
end

Thanks,
Pete
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2007-04-17 01:19
(Received via mailing list)
On 4/16/07, Peter Havens <peter.havens@gmail.com> wrote:
> In RSpec, is there a way to verify that a mock object method was
> called with a block? For example, something like this:
>
> context "A mock object method" do
>   specify "should verify that it was called with a block" do
>     m = mock( "mock object" )
>     m.should_receive( :test_method ).with( :block )
>     m.test_method { :test_result }
>   end
> end

Officially, no. However we're in mid-stream towards supporting
something like this for 0.9 (currently available as a beta gem from .
Basically, anything you can say after "should " or "should_not " you
can pass to #with on a mock. So, in this case, because you can say:

obj.should be_an_instance_of(Proc)

you can also say:

m.should_receive( :test_method ).with(be_an_instance_of(Proc))

We haven't worked out the details of how we'll support this using
different names just yet, so for now you have to either use this
basically un-readable option (that works) or wrap it yourself in
something more pleasant.

Sooner or later we'll support this officially w/ nicer, more readable
names.

Hope this helps in the mean time.

Cheers,
David
This topic is locked and can not be replied to.