Fix `any_instance` so that it updates already stubbed instances
Created by: myronmarston
This fixes #613 (closed).
Note that this changes some behavior put in place by @JonRowe in f585362c (#402)...but I think this provides a better, more consistent/generic solution to the problem that was solving. I may be missing something, though.
The change I'm referring to is easily shown by this diff in one of the specs:
- it "does not remove any stubs set directly on an instance" do
+ it "removes any stubs set directly on an instance" do
allow_any_instance_of(klass).to receive(:existing_method).and_return(:any_instance_value)
obj = klass.new
allow(obj).to receive(:existing_method).and_return(:local_method)
allow_any_instance_of(klass).to receive(:existing_method).and_call_original
- expect(obj.existing_method).to eq(:local_method)
+ expect(obj.existing_method).to eq(:existing_method_return_value)
end
Previously, unstub
(and and_call_original
) would not affect instances that had the method directly stubbed. This kinda sorta made sense since a stub on a particular instance is more specific than an any_instance
stub.
However, I think it's far simpler and more consistent (and less code, too) to make any_instance
affect ALL instances, even those that have been individually stubbed. It's basically "last specified stub wins", just like it is with an individual instance.
What do others think about this change? (And the implementation of the fix, too, of course!)
/cc @JonRowe @cupakromer @samphippen @xaviershay