diff --git a/lib/patch.ex b/lib/patch.ex index e24c6b9..b530556 100644 --- a/lib/patch.ex +++ b/lib/patch.ex @@ -489,7 +489,14 @@ defmodule Patch do """ @spec patch(module :: module(), function :: atom(), value :: Value.t()) :: Value.t() def patch(module, function, %value_module{} = value) when is_value(value_module) do - {:ok, _} = Patch.Mock.module(module) + case Patch.Mock.module(module) do + {:ok, _} -> + :ok + + {:error, :nofile} -> + raise %UndefinedFunctionError{module: module, function: function, arity: 0} + end + :ok = Patch.Mock.register(module, function, value) value end diff --git a/test/user/patch/unknown_module_test.exs b/test/user/patch/unknown_module_test.exs new file mode 100644 index 0000000..05bcde8 --- /dev/null +++ b/test/user/patch/unknown_module_test.exs @@ -0,0 +1,16 @@ +defmodule Patch.Test.User.Patch.UnknownModuleTest do + use ExUnit.Case + use Patch + + describe "patching unknown module" do + test "results in an exception" do + assert_raise UndefinedFunctionError, fn -> + patch(NoSuchModule, :function_that_does_not_exist, :patched) + end + + assert_raise UndefinedFunctionError, fn -> + patch(NoSuchModule, :function_that_does_not_exist, fn _x -> 42 end) + end + end + end +end