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

**URL:** <https://www.ruby-forum.com/t/verifying-that-rspec-mock-object-methods-are-called-with-a-b/92198>\
**Category:** Ruby\
**Created:** [April 17, 2007, 12:25am UTC](https://www.ruby-forum.com/t/verifying-that-rspec-mock-object-methods-are-called-with-a-b/92198 "2007-04-17T00:25:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [April 17, 2007, 12:25am UTC](https://www.ruby-forum.com/t/verifying-that-rspec-mock-object-methods-are-called-with-a-b/92198/1 "2007-04-17T00:25:54Z")

</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  
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

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [April 17, 2007, 1:19am UTC](https://www.ruby-forum.com/t/verifying-that-rspec-mock-object-methods-are-called-with-a-b/92198/2 "2007-04-17T01:19:58Z")

</div>

On 4/16/07, Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
