How to allow stub to return the instance on which it is called, in case of using any_instance?

For example, I have class A.

class A end 

And I want the method to be launched on the instance instance of this class.

 A.any_instance.stub(:my_method).and_return(<here is the same instance on which my_method is called should got>) 

Is it possible to do something similar in RSpec?

+4
source share
1 answer

This will help you:

 A.any_instance.stub(:my_method) do |*args| instance = RSpec::Mocks::space.send(:receivers).last end 

I dug this from rspec code here: rspec github code

PS: It depends entirely on the implementation of rspec and may change in the future. The background is a little hard to explain here. I can add this in the future.

+1
source

Source: https://habr.com/ru/post/1439126/


All Articles