diff --git a/lib/mox.ex b/lib/mox.ex index 27e034b..22f0001 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -780,9 +780,11 @@ defmodule Mox do @spec verify_on_exit!(term()) :: :ok def verify_on_exit!(_context \\ %{}) do pid = self() + NimbleOwnership.set_owner_to_manual_cleanup(@this, pid) ExUnit.Callbacks.on_exit(Mox, fn -> - verify_mock_or_all!(pid, :all) + __verify_mock_or_all__(pid, :all) + NimbleOwnership.cleanup_owner(@this, pid) end) end @@ -792,7 +794,7 @@ defmodule Mox do """ @spec verify!() :: :ok def verify! do - verify_mock_or_all!(self(), :all) + __verify_mock_or_all__(self(), :all) end @doc """ @@ -801,10 +803,12 @@ defmodule Mox do @spec verify!(t()) :: :ok def verify!(mock) do validate_mock!(mock) - verify_mock_or_all!(self(), mock) + __verify_mock_or_all__(self(), mock) end - defp verify_mock_or_all!(owner_pid, mock_or_all) do + # Made public for testing. + @doc false + def __verify_mock_or_all__(owner_pid, mock_or_all) do all_expectations = NimbleOwnership.get_owned(@this, owner_pid, _default = %{}, @timeout) pending = diff --git a/mix.exs b/mix.exs index f01b98b..9ae6e5a 100644 --- a/mix.exs +++ b/mix.exs @@ -30,7 +30,7 @@ defmodule Mox.MixProject do defp deps do [ - {:nimble_ownership, "~> 0.2.0"}, + {:nimble_ownership, "~> 0.3.0"}, {:ex_doc, "~> 0.16", only: :docs} ] end diff --git a/mix.lock b/mix.lock index ddb068c..36d7fbb 100644 --- a/mix.lock +++ b/mix.lock @@ -4,6 +4,6 @@ "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"}, "makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"}, "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, - "nimble_ownership": {:hex, :nimble_ownership, "0.2.0", "5f09a97ce97a873945be4fe52c583f5649954109e7290b11809d7e3271222f96", [:mix], [], "hexpm", "81fa952d95717ca7ee55231f01374078464436aeab49a848121e5602e84cd97e"}, + "nimble_ownership": {:hex, :nimble_ownership, "0.3.0", "29514f8779b26f50f9c07109677c98c0cc0b8025e89f82964dafa9cf7d657ec0", [:mix], [], "hexpm", "76c605106bc1e60f5b028b20203a1e0c90b4350b08e4b8a33f68bb50dcb6e837"}, "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, } diff --git a/test/mox_test.exs b/test/mox_test.exs index 906f2bd..aa88a84 100644 --- a/test/mox_test.exs +++ b/test/mox_test.exs @@ -638,6 +638,26 @@ defmodule MoxTest do Task.await(task) end + + test "raises if the mocks are not called" do + pid = self() + + verify_on_exit!() + + # This replicates exactly what verify_on_exit/1 does, but it adds an assertion + # in there. There's no easy way to test that something gets raised in an on_exit + # callback. + ExUnit.Callbacks.on_exit(Mox, fn -> + assert_raise Mox.VerificationError, fn -> + Mox.__verify_mock_or_all__(pid, :all) + NimbleOwnership.cleanup_owner({:global, Mox.Server}, pid) + end + end) + + set_mox_private() + + expect(CalcMock, :add, fn x, y -> x + y end) + end end describe "stub/3" do