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

Fix stub with undefined #56

Merged
merged 2 commits into from
Sep 3, 2023
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
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
- name: Install OTP and Elixir
uses: erlef/setup-elixir@v1
with:
otp-version: 23.1.1
elixir-version: 1.11.2
otp-version: 24.3
elixir-version: 1.13.3

- name: Install dependencies
run: mix deps.get
Expand All @@ -37,8 +37,6 @@ jobs:
- otp: 24.3
elixir: 1.13.3
coverage: true
- otp: 23.1.1
elixir: 1.11.3
- otp: 24.3
elixir: 1.12.3
env:
Expand Down
8 changes: 8 additions & 0 deletions lib/mimic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ defmodule Mimic do
"""
@spec stub_with(module(), module()) :: module()
def stub_with(module, mocking_module) do
raise_if_module_not_defined!(mocking_module)

module
|> Server.stub_with(mocking_module)
|> validate_server_response(:stub)
Expand Down Expand Up @@ -468,6 +470,12 @@ defmodule Mimic do
end
end

defp raise_if_module_not_defined!(module) do
unless Code.ensure_loaded?(module) do
raise ArgumentError, "Module #{inspect(module)} not defined"
end
end

defp validate_server_response({:ok, module}, _action), do: module

defp validate_server_response({:error, :not_global_owner}, :reject) do
Expand Down
18 changes: 17 additions & 1 deletion test/mimic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ defmodule Mimic.Test do
end
end

describe "stub_with/1 private mode" do
describe "stub_with/2 private mode" do
setup :set_mimic_private

test "called multiple times" do
Expand All @@ -127,6 +127,22 @@ defmodule Mimic.Test do

assert_raise Mimic.UnexpectedCallError, fn -> Calculator.mult(4, 9) end
end

test "undefined mocking module" do
assert_raise ArgumentError,
"Module MissingModule not defined",
fn ->
stub_with(Calculator, MissingModule)
end
end

test "undefined mocked module" do
assert_raise ArgumentError,
"Module MissingModule has not been copied. See docs for Mimic.copy/1",
fn ->
stub_with(MissingModule, InverseCalculator)
end
end
end

describe "stub/3 private mode" do
Expand Down
Loading