I have an application and I am trying to test some of its components. To do this, I am using mix test which is causing me trouble.
Application
I noticed that mix test first launches the entire app before running any tests, even though no tests require the full app to be launched. This is causing me issues because some of the tests require processes to be registered in the Registry, but the tests fail because the ids of those processes are already started.
I can use --no-start, but if I do so, all my tests fail because Mox isn’t launched either.
Is there a way to fix this? I didn’t find anything in the documentation
I don’t want the application to be launched, but I do want the testing libraries to be launched. How am I supposed to test if --no-start doesn’t launch my testing dependencies?
I would have the application start as per usual but start new and unnamed versions of any process you want to test in the test itself.
It’s useful not to hard-code process names for this reason
This is also nice because it allows your to run tests concurrently even if they both leverage the same kind of process because there no longer is a dependancy on global mutable state. It’ll save you from having to do lots of teardown code to ensure the global state is reset between each test too
You will have to launch it explicitly by doing Application.ensure_all_started(:mox) and so on for every dependency.
In my opinion, not starting your application in your tests because you want to manually start a process with the same name is somewhat a bad practice. Instead, I would make sure my API allows processes to be started with a custom name, so you do:
Worker.start_link(:my_name_during_test, ...)
And I tend to use the test name as the name of the process itself:
test "my test", config do
Worker.start_link(config.test, ...)
end
This works nicely if you are starting the process from setup too.