# Verifying that RSpec mock object methods are called with a block

**URL:** <https://rubytalk.org/t/verifying-that-rspec-mock-object-methods-are-called-with-a-block/36983>\
**Category:** ruby-talk\
**Created:** [16 April 2007 22:25 UTC](https://rubytalk.org/t/verifying-that-rspec-mock-object-methods-are-called-with-a-block/36983 "2007-04-16T22:25:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Havens](https://avatars.discourse-cdn.com/v4/letter/p/a5b964/32.png) [@Peter\_Havens](https://rubytalk.org/u/Peter_Havens)\
**Post date:** [16 April 2007 22:25 UTC](https://rubytalk.org/t/verifying-that-rspec-mock-object-methods-are-called-with-a-block/36983/1 "2007-04-16T22:25:05Z")

</div>

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  
&nbsp;&nbsp;specify "should verify that it was called with a block" do  
&nbsp;&nbsp;&nbsp;&nbsp;m = mock( "mock object" )  
&nbsp;&nbsp;&nbsp;&nbsp;m.should\_receive( :test\_method ).with( :block )  
&nbsp;&nbsp;&nbsp;&nbsp;m.test\_method { :test\_result }  
&nbsp;&nbsp;end  
end

Thanks,  
Pete

---

<div class="post-metadata">

**Author:** ![David\_Chelimsky2](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@David\_Chelimsky2](https://rubytalk.org/u/David_Chelimsky2)\
**Post date:** [16 April 2007 23:19 UTC](https://rubytalk.org/t/verifying-that-rspec-mock-object-methods-are-called-with-a-block/36983/2 "2007-04-16T23:19:01Z")

</div>

> 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  
> &nbsp;&nbsp;specify "should verify that it was called with a block" do  
> &nbsp;&nbsp;&nbsp;&nbsp;m = mock( "mock object" )  
> &nbsp;&nbsp;&nbsp;&nbsp;m.should\_receive( :test\_method ).with( :block )  
> &nbsp;&nbsp;&nbsp;&nbsp;m.test\_method { :test\_result }  
> &nbsp;&nbsp;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

> **···**
>
> On 4/16/07, Peter Havens \<peter.havens@gmail.com\> wrote:
> 
> > Thanks,  
> > Pete
