Closed sgilson closed 5 years ago
That's because the start_supervised!
processes are terminated only after the test process is done. By then, the process that defines the mocks is done, which means the mocks are no longer accessible. Your workaround is correct in this case.
Makes perfect sense. Is this behavior also expected when stop_supervised
is explicitly called inside the test? I would think that the test process would still be alive, but this does not seem to be the case...?
No, not really. So my understanding is actually wrong. I will have to investigate this then. Thanks for the report.
So the error is that you were actually not stopping the GenServer. It was returning {:error, :not_found}. You must stop it using its ID, which by default is the module name:
stop_supervised(TeardownGenServer)
I came across an incompatibility with
ExUnit.Callbacks.start_supervised/2
andstop_supervised/1
in which expectations are not shared with the supervised process while executing the teardown logic of a GenServer. I was testing a GenServer that must (attempt) to send an HTTP request during shutdown and all mocks outside of the teardown logic worked exactly as expected. Here is a minimal program that can reproduce the issue.This will lead to a curious result:
The TeardownGenServer had access to the
start/0
mock, but notstop/0
even though it is the same process. I attempted to remedy this issue with many combinations of allowances and expectation placement, but I could simply not get Mox to recognize the expectations withinstop_supervised
without resorting to some nasty global mocks that I won't share here.Workaround: use
GenServer
methods to start and stop the server under test and avoid the ExUnit callbacks when managing processes.