mocks.cr icon indicating copy to clipboard operation
mocks.cr copied to clipboard

Mocking all occurrences of a given instance/class/module

Open nunopato opened this issue 9 years ago • 1 comments

Hi :)

is there a way of mocking, let's say, all the instances of a given class ?

This example:

example = Example.new
allow(example).to receive(say_hello("world")).and_return("hello, world!")

does mock the example instance explicitly created here. But if I want to call a given method and mock all the Example instances created there, I can't.

Let me know if this is something you see fit.

Best, Nuno

nunopato avatar Oct 24 '16 13:10 nunopato

@nunopato Unfortunately no. Also, it would be pretty disastrous for your application design. If you need to mock all instances, then you probably better off using the dependency injection.

Great read on the topic: http://solnic.eu/2013/12/17/the-world-needs-another-post-about-dependency-injection-in-ruby.html

waterlink avatar Apr 08 '17 09:04 waterlink