Last active
May 15, 2020 20:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/config.exs | |
import Config | |
config :ex_tldr, http_client: HTTPoison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/test.exs | |
import Config | |
config :ex_tldr, http_client: ClientMock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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