Skip to content

Commit

Permalink
Return {:error, :already_mocked} when using new/1,2 over mock (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ferraz-oliveira authored Jun 13, 2024
1 parent dd887f3 commit 871c520
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/nuntiux.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ defmodule Nuntiux do
when process_name: process_name(),
opts: opts(),
ok: :ok,
error: {:error, :not_found}
error: {:error, :not_found | :already_mocked}
def new(process_name, opts \\ %{}) do
Nuntiux.Supervisor.start_mock(process_name, opts)
case mocked_process(process_name) do
{:error, :not_mocked} ->
Nuntiux.Supervisor.start_mock(process_name, opts)

_pid ->
{:error, :already_mocked}
end
end

@doc """
Expand All @@ -105,6 +111,9 @@ defmodule Nuntiux do
{:error, :not_found} ->
raise(Nuntiux.Exception, message: "Process #{process_name} not found.")

{:error, :already_mocked} ->
raise(Nuntiux.Exception, message: "Process #{process_name} is already mocked.")

:ok ->
process_name
end
Expand Down
9 changes: 9 additions & 0 deletions test/nuntiux_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ defmodule NuntiuxTest do
assert captured_log =~ "expect_id: :no_head_matches"
assert captured_log =~ ":function_clause"
end

test "new!/1 raises an exception if trying to mock a mock", %{
plus_oner_name: plus_oner_name
} do
assert_raise(Nuntiux.Exception, ~r/^Process .* is already mocked\.$/, fn ->
Nuntiux.new!(plus_oner_name)
Nuntiux.new!(plus_oner_name)
end)
end
end

defp send2(dest, msg) do
Expand Down

0 comments on commit 871c520

Please # to comment.