How to mock HTTPoison with Mox?

You can wrap HTTPoison with your own module.

This works for me:

define a behaviour

defmodule MyApp.Http.Adapter do
  @callback get(url :: String.t(), headers :: list) :: String.t()

  @callback post(url :: String.t(), body :: String.t(), headers :: list) :: String.t()
end

define the real implementation

defmodule MyApp.Http.Client do

  def get(url, headers) do
    response = HTTPoison.get(.....)
    ...
  end

  def post(url, body, headers) do
    ...
  end

in config/test.exs

config :my_app, :http_adapter, MyApp.Http.Mock

for dev/prod

config :my_app, :http_adapter, MyApp.Http.Client

usage:

defmodule MyApp.Foo do
  @http_client Application.get_env(:my_app, :http_adapter)

  def bar() do
      @http_client.post(url, payload, [])
  end
end

in your test:

    MyApp.Http.Mock
    |> expect(:post, fn _url, _body, _headers ->
      {:ok, %HTTPoison.Response{status: 200, body: ....}}
    end)

Hope this helps.

3 Likes