I’ve had a heck of a time testing API calls in Elixir. It is not like testing API calls in Ruby.
But, I did get some tests to pass! They just use actual network calls for now.
What I struggled with:
ENV variables don’t seem to be working when running tests. I can launch
iexin the test environment and get them to work, but not when executing tests. I know it has something to do with compilation, but I don’t know exactly what yet.
I have a cache that runs before the app starts. That doesn’t seem to work with tests either. It’s sort of a good thing because it forced me to guard against nil values, but it should work.
What I learned:
- Elixir tests are a lot different than Ruby tests.
- Elixir code needs to be designed in a way that limits scope. Testing is good for exposing that.
- There are some different opinions on how to mock things in the Elixir community. Somewhat related to Ruby, but never something I ever dug into too deeply with in Ruby.
Questions I still have:
What is the preferred way to test APIs in Elixir? I can mock out requests or create dummy services. I’m not sure which approach is best.
What’s the best way to have ENV variables used in testing? I would like to avoid all network requests in tests, but storing them locally seems to fail for me.