Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

and_call_original used with allow_any_instance_of is not working after rspec upgrade to 3.1.0

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?

like image 896
clj Avatar asked Oct 26 '25 03:10

clj


1 Answers

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.

like image 110
clj Avatar answered Oct 28 '25 15:10

clj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!