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

Allow defmock_of to specify the module name #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
15 changes: 12 additions & 3 deletions lib/elixir_mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,16 @@ defmodule ElixirMock do

For more on the options available within mock definitions, see `defmock_of/2`
"""
defmacro defmock_of(real_module, context \\ {:%{}, [], []}, do: mock_ast) do
defmacro defmock_of(real_module, opts \\ [context: {:%{}, [], []}, mock_name: nil], do: mock_ast) do
{context, mock_name} = case opts do
m when is_map(m) ->
{m, nil}
t when is_tuple(t) ->
{t, nil}
l when is_list(l) ->
{Keyword.get(l, :context, {:%{}, [], []}), Keyword.get(l, :mock_name)}
end

call_through_unstubbed_fns = call_through_unstubbed_functions?(mock_ast)
mock_fns = extract_mock_fns(mock_ast)
stubs = Enum.map mock_fns, fn {_fn_type, {name, arity}} -> {name, arity} end
Expand All @@ -174,7 +183,7 @@ defmodule ElixirMock do
mock_ast = unquote(Macro.escape(mock_ast))
call_through_unstubbed_fns = unquote(call_through_unstubbed_fns)

mock_module_name = random_module_name()
mock_module_name = unquote(mock_name) || random_module_name()

verify_mock_structure(mock_fns, real_module)

Expand Down Expand Up @@ -548,4 +557,4 @@ defmodule ElixirMock do
|> Enum.map(fn(fn_ast) -> apply_stub_call_throughs(fn_ast, real_module) end)
end

end
end