Skip to content
Open
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
9 changes: 8 additions & 1 deletion lib/patch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/user/patch/unknown_module_test.exs
Original file line number Diff line number Diff line change
@@ -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