Mock in multiple tests with mock library?

I need to set a mock for multiple tests but I don’t want to repeat it in each test

You may want to checkout this article on Mocks by Plataformatec, which is basically required reading on the subject

Sorry for resurrecting this thread. I’ve read that post and, if I understood it correctly, two approaches are explained there:

  1. Configure the dependency via config files.
  2. Pass it as a parameter to the each function.

In (1) it seems to be too generic. The dependency definition is “too far” from its use. I find it hard to maintain.
In (2) there is a lot of parameter repetition.

Sometimes I think it would be a good idea to have some kind of state to store the dependencies, but this would prevent running tests in parallel.

So, how do you guys approach this problem?

Thank you.

If tests should run in parallel you need to pass in dependencies via functions’ parameters. You could also use the process dictionary and count on the fact that each test runs as it’s own process (this is what Mox or Ecto.Sandbox does), but if your test does delegate work to other processes this will require extra work for parallel tests.

1 Like

Yes, that’s a good idea, to pass the state with the dependencies. Actually, an ETS might hold the dependencies and then, as you said, to pass the keys through the different processes.

I’ll try that. Thanks for the answer.