diff --git a/lib/mox.ex b/lib/mox.ex index 27e034b..29a5bbd 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -804,7 +804,9 @@ defmodule Mox do 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/test/mox_test.exs b/test/mox_test.exs index 906f2bd..f1bf08a 100644 --- a/test/mox_test.exs +++ b/test/mox_test.exs @@ -640,6 +640,25 @@ defmodule MoxTest do end end + describe "verify_on_exit!/0 with errors" do + test "raises if the mocks are not called" do + pid = self() + + # 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 -> + verify_mock_or_all!(pid, :all) + end + end) + + set_mox_private() + + expect(CalcMock, :add, fn x, y -> x + y end) + end + end + describe "stub/3" do test "allows repeated invocations" do in_all_modes(fn ->