Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functioning barebones Mox implementation example #59

Merged
merged 4 commits into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/barebones_with_setup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# MoxExample

## Description

This example is a barebones example of how to use [Mox](https://github.com/plataformatec/mox) and how to configure it during testing.

## How

Run `mix test` and you'll see a mocked HTTP call tested without making the HTTP call.

Run `iex -S mix` and then `MoxExample.post_name("Mox")` and you'll see the HTTP request go through!
30 changes: 30 additions & 0 deletions examples/barebones_with_setup/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure your application as:
#
# config :mox_example, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:mox_example, :key)
#
# You can also configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
import_config "#{Mix.env()}.exs"
1 change: 1 addition & 0 deletions examples/barebones_with_setup/config/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use Mix.Config
4 changes: 4 additions & 0 deletions examples/barebones_with_setup/config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Mix.Config
# this line configures our :example_api to use the ExampleAPIMock defined in
# test/support/mocks.ex
config :mox_example, :example_api, ExampleAPIMock
20 changes: 20 additions & 0 deletions examples/barebones_with_setup/lib/mox_example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule MoxExample do
@example_api Application.get_env(
:mox_example,
:example_api,
ExampleAPI
)
def post_name(name) do
# run api-post/3 in our currently selected implementation
@example_api.api_post(name, [], [])
end
end

defmodule ExampleAPI do
alias HTTPoison
@callback api_post(String.t(), [], []) :: {:ok, nil} | {:error, any()}
def api_post(body \\ "", headers, options) do
# if I don't mock this then this is an integration test!
HTTPoison.post("http://example.com", body, headers, options)
end
end
35 changes: 35 additions & 0 deletions examples/barebones_with_setup/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule MoxExample.MixProject do
use Mix.Project

def project do
[
app: :mox_example,
version: "0.1.0",
elixir: "~> 1.7",
start_permanent: Mix.env() == :prod,
deps: deps(),
elixirc_options: [warnings_as_errors: true],
elixirc_paths: elixirc_paths(Mix.env())
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# ensure test/support is compiled
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]


# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:httpoison, "~> 1.4"},
{:mox, "~> 0.4", only: :test}
]
end
end
12 changes: 12 additions & 0 deletions examples/barebones_with_setup/mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%{
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.15.1", "9f8f471c844b8ce395f7b6d8398139e26ddca9ebc171a8b91342ee15a19963f4", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "1.5.0", "71ae9f304bdf7f00e9cd1823f275c955bdfc68282bc5eb5c85c3a9ade865d68e", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
"mox": {:hex, :mox, "0.5.0", "c519b48407017a85f03407a9a4c4ceb7cc6dec5fe886b2241869fb2f08476f9e", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
}
16 changes: 16 additions & 0 deletions examples/barebones_with_setup/test/mox_example_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule MoxExampleTest do
import Mox

use ExUnit.Case
doctest MoxExample

setup :verify_on_exit!

test "posts name" do
name = "Jim"
ExampleAPIMock |>
expect(:api_post, fn ^name, [], [] -> {:ok, nil} end)

assert MoxExample.post_name(name) == {:ok, nil}
end
end
1 change: 1 addition & 0 deletions examples/barebones_with_setup/test/support/mocks.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mox.defmock(ExampleAPIMock, for: ExampleAPI)
4 changes: 4 additions & 0 deletions examples/barebones_with_setup/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this may be needed for some older versions of elixir
#{:ok, _} = Application.ensure_all_started(:mox)
SophisticaSean marked this conversation as resolved.
Show resolved Hide resolved

ExUnit.start()