Hi everyone, I’ve been reading the PragProg ‘Programmin Elixir’ book, and have just completed Chapter 20 (building a simple file duplication app). I’ve got the code here: https://github.com/iarekk/elixir-duper/.
I’ve been really struggling with unit testing this project though. In my customary world (C#/.NET backend dev), a suite of unit tests would not be starting the application, but rather running individual method/class tests in isolation. Anything that talks to external file systems/DBs/services would be mocked, so unit tests run trivially on an air-gapped build machine.
This is not how Elixir seems to be operating.
The default behaviour is to start the full application when mix test
is called. I like this approach as it’s immediately testing the supervision trees, and that all the modules are in place.
How is that supposed to work on a build machine though? Any real-world app will have external dependencies/services/secrets that are only available once you’re deployed to the correct env (dev/staging/prod), and shouldn’t be visible from the build agent.
Are my services supposed to detect they are on a build machine and fall back to some ‘mock’ implementations?
For now, I’ve managed to get my (pure) unit tests working by
- stopping the application from starting
mix test --no-start
- getting the PID of my service using the
start_supervised!/2
service - this is the PR https://github.com/iarekk/elixir-duper/pull/2/files#diff-57ab11b48e6e385aa43248d3839e1e5381e4795308dcd2521d74388f39bfc0d4
I feel I’m fighting the ecosystem more than I’m using it at the moment. Is what I’m doing idiomatic to Elixir?