TLDR; I really just want to test that my module called a function on another module, but so that it still works in parallel tests so preferably doesn’t use stubbing/mocking (or at least not as I know it)
More concrete example:
I have a module that some times needs to print something with
IO.puts - it has gotten a bit much and not really pure so I thought that I might want to extract it into a separate module to separate concerns and help my testing be without
capture_io. And then sort of do “dependency injection” of the module. So something like:
defmodule MyModule do def main_function(parameter, printer \\ DefaultPrinter), do: ... end
And in the tests I would then use a different module that doesn’t print for real:
But, I still want to assert that a certain method was called (maybe with certain parameters) which is important to know as it’s an important property of my system that this method is called (printing out a warning for instance).
Of course, you might feel my OOP roots - what I’d want to do is stubbing/mocking to assert that something has been called. But stubbing/mocking is not really elixir-ish as it’s global state and that would also prevent running the tests in parallel.
So, what do I do? Right now I’m thinking about just still using
capture_io when I care about if it prints or not with the default module and just using a fake “do nothing module” when I don’t care in the tests.
Any other ideas? All input appreciated