mocks.cr
mocks.cr copied to clipboard
Mocking all occurrences of a given instance/class/module
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 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