Skip to content

Commit

Permalink
Fix error message when Mimic.copy has not been called on a module
Browse files Browse the repository at this point in the history
  • Loading branch information
brain-geek authored and edgurgel committed Jul 16, 2024
1 parent f7da91f commit 738ed32
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/mimic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ defmodule Mimic do
end

defp raise_if_not_exported_function!(module, fn_name, arity) do
# This can return {:error, :nofile}, but we don't care about that
Code.ensure_loaded(module)

unless function_exported?(module, fn_name, arity) do
raise ArgumentError, "Function #{fn_name}/#{arity} not defined for #{inspect(module)}"
end
Expand Down
24 changes: 12 additions & 12 deletions test/mimic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ defmodule Mimic.Test do

test "raises if a non copied module is given" do
assert_raise ArgumentError,
"Module String has not been copied. See docs for Mimic.copy/1",
"Module NotCopiedModule has not been copied. See docs for Mimic.copy/1",
fn ->
stub(String, :split, fn x, y -> x + y end)
stub(NotCopiedModule, :inc, fn x -> x - 1 end)
end
end

Expand Down Expand Up @@ -356,9 +356,9 @@ defmodule Mimic.Test do

test "raises if a non copied module is given" do
assert_raise ArgumentError,
"Module String has not been copied. See docs for Mimic.copy/1",
"Module NotCopiedModule has not been copied. See docs for Mimic.copy/1",
fn ->
stub(String, :split, fn x, y -> x + y end)
stub(NotCopiedModule, :inc, fn x -> x - 1 end)
end
end

Expand Down Expand Up @@ -436,9 +436,9 @@ defmodule Mimic.Test do

test "raises if a non copied module is given" do
assert_raise ArgumentError,
"Module String has not been copied. See docs for Mimic.copy/1",
"Module NotCopiedModule has not been copied. See docs for Mimic.copy/1",
fn ->
expect(String, :split, fn x, y -> x + y end)
stub(NotCopiedModule, :inc, fn x -> x - 1 end)
end
end

Expand Down Expand Up @@ -569,9 +569,9 @@ defmodule Mimic.Test do

test "raises if a non copied module is given" do
assert_raise ArgumentError,
"Module String has not been copied. See docs for Mimic.copy/1",
"Module NotCopiedModule has not been copied. See docs for Mimic.copy/1",
fn ->
expect(String, :split, fn x, y -> x + y end)
stub(NotCopiedModule, :inc, fn x -> x - 1 end)
end
end

Expand Down Expand Up @@ -607,9 +607,9 @@ defmodule Mimic.Test do

test "raises if a non copied module is given" do
assert_raise ArgumentError,
"Module String has not been copied. See docs for Mimic.copy/1",
"Module NotCopiedModule has not been copied. See docs for Mimic.copy/1",
fn ->
reject(String, :split, 2)
stub(NotCopiedModule, :inc, fn x -> x - 1 end)
end
end

Expand Down Expand Up @@ -680,9 +680,9 @@ defmodule Mimic.Test do

test "raises if a non copied module is given" do
assert_raise ArgumentError,
"Module String has not been copied. See docs for Mimic.copy/1",
"Module NotCopiedModule has not been copied. See docs for Mimic.copy/1",
fn ->
reject(String, :split, 2)
stub(NotCopiedModule, :inc, fn x -> x - 1 end)
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/support/test_modules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ defmodule NoStubs do
def add(x, y), do: x + y
end

defmodule NotCopiedModule do
@moduledoc false
def inc(counter), do: counter - 1
end

defmodule Structs do
@moduledoc false
@enforce_keys [:foo, :bar]
Expand Down

0 comments on commit 738ed32

Please sign in to comment.