Skip to content

Commit

Permalink
Change return type of mock/1 & global_mock/1
Browse files Browse the repository at this point in the history
  • Loading branch information
teamon committed Jul 26, 2021
1 parent 74d300e commit d930665
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
25 changes: 17 additions & 8 deletions lib/tesla/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,23 @@ defmodule Tesla.Mock do
This mock will only be available to the current process.
"""
@spec mock((Tesla.Env.t() -> Tesla.Env.t() | {integer, map, any})) :: no_return
def mock(fun) when is_function(fun), do: pdict_set(fun)
@spec mock((Tesla.Env.t() -> Tesla.Env.t() | {integer, map, any})) :: :ok
def mock(fun) when is_function(fun) do
pdict_set(fun)
:ok
end

@doc """
Setup global mocks.
**WARNING**: This mock will be available to ALL processes.
It might cause conflicts when running tests in parallel!
"""
@spec mock_global((Tesla.Env.t() -> Tesla.Env.t() | {integer, map, any})) :: no_return
def mock_global(fun) when is_function(fun), do: agent_set(fun)
@spec mock_global((Tesla.Env.t() -> Tesla.Env.t() | {integer, map, any})) :: :ok
def mock_global(fun) when is_function(fun) do
agent_set(fun)
:ok
end

## HELPERS

Expand Down Expand Up @@ -225,10 +231,13 @@ defmodule Tesla.Mock do
defp agent_set(fun) do
case Process.whereis(__MODULE__) do
nil ->
ExUnit.Callbacks.start_supervised!(%{
id: __MODULE__,
start: {Agent, :start_link, [fn -> fun end, [{:name, __MODULE__}]]}
})
ExUnit.Callbacks.start_supervised!(
%{
id: __MODULE__,
start: {Agent, :start_link, [fn -> fun end, [{:name, __MODULE__}]]}
},
[]
)

pid ->
Agent.update(pid, fn _ -> fun end)
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ defmodule Tesla.Mixfile do
lockfile: lockfile(System.get_env("LOCKFILE")),
test_coverage: [tool: ExCoveralls],
dialyzer: [
plt_add_apps: [:inets, :idna, :ssl_verify_fun],
plt_core_path: "_build/#{Mix.env()}",
plt_add_apps: [:mix, :inets, :idna, :ssl_verify_fun, :ex_unit],
plt_add_deps: :project
],
docs: docs()
Expand Down

0 comments on commit d930665

Please sign in to comment.