Mock v0.3.3 Mock

Mock modules for testing purposes. Usually inside a unit test.

Please see the README file on github for a tutorial

Example

defmodule MyTest do
  use ExUnit.Case
  import Mock

  test "get" do
    with_mock HTTPotion,
        [get: fn("http://example.com", _headers) ->
                HTTPotion.Response.new(status_code: 200,
                    body: "hello") end] do
      # Code which calls HTTPotion.get
      # Check that the call was made as we expected
      assert called HTTPotion.get("http://example.com", :_)
    end
  end
end

Link to this section Summary

Functions

Use inside a with_mock block to determine whether a mocked function was called as expected. If the assertion fails, the calls that were received are displayed in the assertion message

Use inside a with_mock block to determine whether a mocked function was called as expected

Mocks up multiple modules prior to the execution of each test in a case and execute the callback specified

Mocks up multiple modules prior to the execution of each test in a case and execute the callback specified with a context specified

Shortcut to avoid multiple blocks when a test requires a single mock

Shortcut to avoid multiple blocks when a test requires a single mock. Accepts a context argument enabling information to be shared between callbacks and the test

Mock up mock_module with functions specified as a keyword list of function_name:implementation mocks for the duration of test

Mock up multiple modules for the duration of test

Link to this section Functions

Link to this macro assert_called(arg) (macro)

Use inside a with_mock block to determine whether a mocked function was called as expected. If the assertion fails, the calls that were received are displayed in the assertion message.

Pass :_ as a function argument for wildcard matches.

Example

assert_called HTTPotion.get("http://example.com")

# Matches any invocation
assert_called HTTPotion.get(:_)
Link to this macro called(arg) (macro)

Use inside a with_mock block to determine whether a mocked function was called as expected.

Pass :_ as a function argument for wildcard matches.

Example

assert called HTTPotion.get("http://example.com")

# Matches any invocation
assert called HTTPotion.get(:_)
Link to this macro mock_modules(mocks) (macro)
Link to this macro setup_with_mocks(mocks, list) (macro)

Mocks up multiple modules prior to the execution of each test in a case and execute the callback specified.

For full description of mocking, see with_mocks.

For a full description of ExUnit setup, see https://hexdocs.pm/ex_unit/ExUnit.Callbacks.html

Example

setup_with_mocks([
  {Map, [], [get: fn(%{}, "http://example.com") -> "<html></html>" end]}
]) do
  foo = "bar"
  {:ok, foo: foo}
end

test "setup_all_with_mocks base case" do
  assert Map.get(%{}, "http://example.com") == "<html></html>"
end
Link to this macro setup_with_mocks(mocks, context, list) (macro)

Mocks up multiple modules prior to the execution of each test in a case and execute the callback specified with a context specified

See setup_with_mocks for more details

Example

setup_with_mocks([
  {Map, [], [get: fn(%{}, "http://example.com") -> "<html></html>" end]}
], context) do
  {:ok, test_string: Atom.to_string(context.test)}
end

test "setup_all_with_mocks with context", %{test_string: test_string} do
  assert Map.get(%{}, "http://example.com") == "<html></html>"
  assert test_string == "test setup_all_with_mocks with context"
end
Link to this macro test_with_mock(test_name, mock_module, opts \\ [], mocks, test_block) (macro)

Shortcut to avoid multiple blocks when a test requires a single mock.

For full description see with_mock.

Example

test_with_mock "test_name", HTTPotion,
  [get: fn(_url) -> "<html></html>" end] do
  HTTPotion.get("http://example.com")
  assert called HTTPotion.get("http://example.com")
end
Link to this macro test_with_mock(test_name, context, mock_module, opts, mocks, test_block) (macro)

Shortcut to avoid multiple blocks when a test requires a single mock. Accepts a context argument enabling information to be shared between callbacks and the test.

For full description see with_mock.

Example

setup do
  doc = "<html></html>"
  {:ok, doc: doc}
end

test_with_mock "test_with_mock with context", %{doc: doc}, HTTPotion, [],
  [get: fn(_url) -> doc end] do

  HTTPotion.get("http://example.com")
  assert called HTTPotion.get("http://example.com")
end
Link to this macro with_mock(mock_module, opts \\ [], mocks, list) (macro)

Mock up mock_module with functions specified as a keyword list of function_name:implementation mocks for the duration of test.

opts List of optional arguments passed to meck. :passthrough will passthrough arguments to the original module.

Example

with_mock(HTTPotion, [get: fn("http://example.com") ->
     "<html></html>" end] do
   # Tests that make the expected call
   assert called HTTPotion.get("http://example.com")
end
Link to this macro with_mocks(mocks, list) (macro)

Mock up multiple modules for the duration of test.

Example

with_mocks([{HTTPotion, opts, [{get: fn(“http://example.com”) -> “” end}]}]) do # Tests that make the expected call assert called HTTPotion.get(“http://example.com”) end