Unit Testing HTTP Client error response

Hello all,

Let’s say we have a module / wrapper that carries out calls to an external HTTP service to fetch the body of the response, for example:

defmodule RandomThirdPartyClient do

  def perform_request(data) do
    url = "http://www.some-service?q=#{data}"

    case HTTPoison.get(url) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        {:ok, body}

      {:error, %HTTPoison.Error{reason: reason}} ->
        {:error, reason}
    end
  end
end

How would one test that the function returns an error (which would result from a HTTPoison.Error)? Would it be best to:

  • use the package Bypass to simulate lack of connectivity
  • Stubbing the function to return an error

Or something completely different? Or am I missing the point entirely here? I ask because excoveralls test coverage requirement.

Thanks.

p.s I currently use exVCR to test the happy path to avoid carrying out actual requests.