In recent discussions with one of our Elixir Development Subscription clients, the topic of HTTP API client testing came up. In this article weβll walk through an imaginary third party API integration and discuss testing approaches and affordances provided by Req.
Imagine weβre building an app that displays the weather for a given location using a third-party HTTP weather service. We might have code similar to this:
defmodule MyApp.Weather dodef get_temperature(location) dooptions = [url: "https://weather-service/temperature",params: [location: location]]# error handling left out for brevity{:ok, %{status: 200, body: %{"celsius" => celsius}}} = Req.request(options){:ok, %{celsius: celsius}}endend
iex> MyApp.Weather.get_temperature("Krakow, Poland"){:ok, %{celsius: 10.0}}Unfortunately this function is not so easy to test because it performs an external API request on every call. Such network requests can be flaky, slow, expensive, etc. A popular solution is to use mocks and stubs: just stub out a function call or two and move on, right? However, exactly because these network requests can be slow & flaky, they tend to be the source of many, if not most, performance and reliability issues in applications. Making these pain points stand out can be helpful. There are also real issues with mocks and stubs described next.
continue reading on dashbit.co
β οΈ This post links to an external website. β οΈ
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.