fireproofsocks

fireproofsocks

Recommended Strategies for testing 3rd Party APIs?

I have a couple Elixir modules that rely in part on 3rd party APIs. I’m wondering how I can best test them? Since the 3rd party service may be down or we may experience time-outs or we may have mis-configured the connection entirely, it is important that our module can recover and log any problems.

One gotcha is that Elixir reads ENV variables at compile-time. If I wanted to test that the proper URL had been entered, I would have to restructure my module and functions to allow for me to inject an override. Is this recommended?

Another not-so-hypothetical case is that our account with the 3rd party may have lapsed – let’s say our contract expired without anyone noticing. In that case, our API token would stop working and their API would start returning an error page. Again, I could refactor our modules so that I could inject a fake response, but it seems like that approach is not idiomatic to Elixir.

Any suggestions? Thanks!

Most Liked

dimitarvp

dimitarvp

Jose wrote about mocks a while ago: Mocks and explicit contracts.

Long story short, it involves configuring implementations of APIs per environment but mandates you create a behaviour which serves as a contract.

As for 3rd party APIs erroring, I am not sure what your question really is; if the API starts erroring out surely you can catch the {:error, reason} tuples in your calling functions and react accordingly? Sorry if I misunderstood your problem there.

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement