From 3bb1d91bf5ba1e34725f66b426a993a160d2cade Mon Sep 17 00:00:00 2001 From: Paulo Daniel Gonzalez Date: Wed, 5 Nov 2025 13:47:57 -0600 Subject: [PATCH 1/3] Add warning to stub/3 --- lib/mox.ex | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/mox.ex b/lib/mox.ex index 2ae708b..2bba59f 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -597,6 +597,17 @@ defmodule Mox do stub(MockWeatherAPI, :get_temp, fn _loc -> {:ok, 30} end) `stub/3` will overwrite any previous calls to `stub/3`. + + > #### ⚠️ Warning {: .warning} + > + > `stub/3` does **not verify that a call occurred**. + > If the production code stops calling the stubbed function + > (for example, after a refactor), the test will still pass. + > + > This can lead to unused stubs lingering in tests and a + > false sense of coverage. Use `expect/3` instead when you + > want to ensure the function is actually invoked, which is + > the preferred approach in most cases. """ @spec stub(mock, atom(), function()) :: mock when mock: t() def stub(mock, name, code) From 5f6f7346927d73f9408ef7bd6938ca44f1d34a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 6 Nov 2025 14:59:58 +0100 Subject: [PATCH 2/3] Update lib/mox.ex --- lib/mox.ex | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/mox.ex b/lib/mox.ex index 2bba59f..8623ad9 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -598,16 +598,15 @@ defmodule Mox do `stub/3` will overwrite any previous calls to `stub/3`. - > #### ⚠️ Warning {: .warning} + > #### Warning {: .warning} > > `stub/3` does **not verify that a call occurred**. - > If the production code stops calling the stubbed function + > If the code stops calling the stubbed function > (for example, after a refactor), the test will still pass. > - > This can lead to unused stubs lingering in tests and a - > false sense of coverage. Use `expect/3` instead when you - > want to ensure the function is actually invoked, which is - > the preferred approach in most cases. + > This can lead to unused stubs lingering in tests. + > Always use `expect/3` when you want to ensure the + > function is actually invoked. """ @spec stub(mock, atom(), function()) :: mock when mock: t() def stub(mock, name, code) From ac5fdf774363bddb4f638a8a9b154eeba2ed77e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 6 Nov 2025 15:00:30 +0100 Subject: [PATCH 3/3] Update lib/mox.ex --- lib/mox.ex | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/mox.ex b/lib/mox.ex index 8623ad9..02a721d 100644 --- a/lib/mox.ex +++ b/lib/mox.ex @@ -598,15 +598,15 @@ defmodule Mox do `stub/3` will overwrite any previous calls to `stub/3`. - > #### Warning {: .warning} - > - > `stub/3` does **not verify that a call occurred**. - > If the code stops calling the stubbed function - > (for example, after a refactor), the test will still pass. - > - > This can lead to unused stubs lingering in tests. - > Always use `expect/3` when you want to ensure the - > function is actually invoked. + > #### Warning {: .warning} + > + > `stub/3` does **not verify that a call occurred**. + > If the code stops calling the stubbed function + > (for example, after a refactor), the test will still pass. + > + > This can lead to unused stubs lingering in tests. + > Always use `expect/3` when you want to ensure the + > function is actually invoked. """ @spec stub(mock, atom(), function()) :: mock when mock: t() def stub(mock, name, code)