From 29e376d0c97630af28e160d4b636c9a81b890b24 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Thu, 7 Mar 2024 11:45:46 +0100 Subject: [PATCH 1/2] Fix verify_on_exit!/1 with nimble_ownership --- lib/mox.ex | 7 +++++-- test/mox_test.exs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/mox.ex b/lib/mox.ex index 27e034b..9bdf73e 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -804,8 +804,11 @@ defmodule Mox do verify_mock_or_all!(self(), mock) end - defp verify_mock_or_all!(owner_pid, mock_or_all) do - all_expectations = NimbleOwnership.get_owned(@this, owner_pid, _default = %{}, @timeout) + # 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 = for {_mock, expected_funs} <- all_expectations, 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 -> From f6dad31700714708f3293ec80d30fb6826c130b5 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Thu, 7 Mar 2024 11:46:11 +0100 Subject: [PATCH 2/2] FIXUP --- lib/mox.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/mox.ex b/lib/mox.ex index 9bdf73e..29a5bbd 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -807,8 +807,7 @@ defmodule Mox 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) + all_expectations = NimbleOwnership.get_owned(@this, owner_pid, _default = %{}, @timeout) pending = for {_mock, expected_funs} <- all_expectations,