I had a code in rspec 2.14.1 like

allow_any_instance_of(AnyClass).to receive(:some_method).and_call_original 

with a corresponding message expectation

expect_any_instance_of(AnyClass).to receive(:some_method) 

The above worked fine in rspec 2.14.1. After upgrade to rspec 3.1.0, the above code no longer works. It fails on the message expectation that some_method is not called even once

However if I change the stub like

allow_any_instance_of(AnyClass).to receive(:some_method).and_return(value) 

it works fine in rspec 3.1.0.

I just wanted to understand why using and_call_original with allow_any_instance_of fails after rspec upgrade.

I can see from this link that and_call_original is only supported on partial doubles.

Does that mean using allow_any_instance_of is not partial double?

1 Answer

and_call_original does actually work when used with allow_any_instance_of.

Refer the specs for any_instance which gives us an idea of different ways of mocking or to un-stub using and_call_original here.

To answer my question above, the way I was using message expectation is wrong. It should be:

allow_any_instance_of(AnyClass).to( receive(:any_method).and_call_original ) expect(AnyClass.new.any_method).to eq(:any_method_value) 

I was trying to use expect_any_instance_of instead of expect which caused the issue.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.