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

Change return type of mock/1 & global_mock/1 #484

Merged
merged 1 commit into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
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
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