First Time Testing in Elixir

May 8, 2020

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:

  1. ENV variables don’t seem to be working when running tests. I can launch iex in 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.

  2. 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:

  1. Elixir tests are a lot different than Ruby tests.
  2. Elixir code needs to be designed in a way that limits scope. Testing is good for exposing that.
  3. 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:

  1. 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.

  2. 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.

More stuff:

or go see everything in the archive.