Skip to content

Instantly share code, notes, and snippets.

@ivanhercaz
Last active May 15, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanhercaz/2d36f64c971d6c88b4cb95b5d7eee39a to your computer and use it in GitHub Desktop.
Save ivanhercaz/2d36f64c971d6c88b4cb95b5d7eee39a to your computer and use it in GitHub Desktop.
Try to use Mox to create a mock to handle errors of HTTPoison
# config/config.exs
import Config
config :ex_tldr, http_client: HTTPoison
# lib/ex_tldr.ex
defmodule ExTldr do
@moduledoc """
Documentation for ExTldr, an Elixir client for tldr-pages.
"""
@http_client Application.get_env(:ex_tldr, :http_client)
# Another functions
defp describe(os, term) do
case @http_client.get(process_url(os, term)) do
# Conditions to check
end
end
end
# test/ex_tldr_test.exs
defmodule ExTldrTest do
use ExUnit.Case
import ExUnit.CaptureIO
alias ExTldr
alias ClientMock
doctest ExTldr
import Mox
setup :verify_on_exit!
# Several tests
test "should return lack of internet" do
assert_raise NoInternetConnectionError,
fn ->
ClientMock
|> expect(:get, fn _ -> {:error, %HTTPoison.Error{reason: :nxdomain}} end)
end
end
end
# config/test.exs
import Config
config :ex_tldr, http_client: ClientMock
# test/test_helper.exs
ExUnit.start()
Mox.defmock(
ClientMock,
for: HTTPoison.Base
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment