silverdr

silverdr

Troubles using Mox library for testing when no Mox expectations are defined

There is the Recaptcha library

https://github.com/samueljseay/recaptcha

which helps me in verifying recaptcha responses. All nice. Time for testing (yeah, after getting code to work somehow – apologies to all TDD fans). Obviously I don’t want to hit uncle google with my tests, so:

Mox.defmock(MyApplication.Accounts.MockRecaptcha, for: MyApplication.Accounts.RecaptchaBehaviour)

inside test_helper.ex. Needed to define that behaviour separately:

defmodule MyApplication.Accounts.RecaptchaBehaviour do
	@callback verify(String.t(), Keyword.t()) :: {:ok, Response.t()} | {:error, [atom]}
	@callback verify(String.t()) :: {:ok, Response.t()} | {:error, [atom]}
end

do some tests using:

MyApplication.Accounts.MockRecaptcha
|> expect(:verify, fn _response -> {:ok, _response} end)

So far so good, except… all other tests are now failing with:

** (Mox.UnexpectedCallError) no expectation defined for MyApplication.Accounts.MockRecaptcha.verify/1 in process #PID<0.854.0> with args [nil]

Reading the fine docs I find: “[…] you might want the implementation to fall back to a stub (or actual) implementation when no expectations are defined. stub_with/2 is just what you need!”

So another line in test_helper.ex:

Mox.stub_with(MyApplication.Accounts.MockRecaptcha, Recaptcha)

That doesn’t work because ** (ArgumentError) Recaptcha does not implement any behaviour, Well.. let’s add my own “proxy” then, which does:

defmodule MyApplication.Accounts.Recaptcha do
	@behaviour MyApplication.Accounts.RecaptchaBehaviour

	def verify(response, options \\ []) do
		Recaptcha.verify(response, options)
	end
end

And change the test_helper.ex line to

Mox.stub_with(MyApplication.Accounts.MockRecaptcha, MyApplication.Accounts.Recaptcha)

Now the ArgumentError is gone but all tests with no Mox expectations fail the same as before. No change with and without the stub_with/2.

And I feel like I spent already far too much time with it… :frowning: Any help to put me on track?

Most Liked

ruslandoga

ruslandoga

I have a telegram bot client in my app which uses both mocks and stubs during tests and it seems to work fine. Here’s it’s setup:

config/test.exs

config :app, App.Bot, adapter: MockBot

lib/bot.ex

defmodule App.Bot do
  # I'm using compile-time configuration
  # https://hexdocs.pm/mox/Mox.html#module-compile-time-requirements
  # it's probably not important
  @adapter Application.compile_env!(:app, [__MODULE__, :adapter])
  
  def set_webhook(url) do
    @adapter.set_webhook(url)
  end
  
  # ...
end

lib/bot/adapter.ex

defmodule App.Bot.Adapter do
  @moduledoc false
  @callback set_webhook(url :: String.t()) :: :ok | {:error, map}
  # ...
end

test/support/mocks.exs

Mox.defmock(MockBot, for: App.Bot.Adapter)

test/support/stub_bot.ex

defmodule StubBot do
  @behaviour App.Bot.Adapter

  @impl true
  def set_webhook(_url), do: :ok

  # ...
end

and finally test/support/data_case.ex and other helper cases for tests that end up touching the bot module have Mox.stub_with/2 in setup:

defmodule App.DataCase do
  use ExUnit.CaseTemplate

  # ...

  setup tags do
    # ...

    Mox.stub_with(MockBot, StubBot)

    # ...
  end
end
silverdr

silverdr

Right - if I put the stub_with into both conn_case and data_case then all tests work normally. IOW – @ruslandoga – you’re my saviour, man! I guess it has to be invoked always in the given test file context. If I now re-read the doc, the wording might actually imply this… gosh!

silverdr

silverdr

Since nobody seems to be able to step-in I added a

TL;DR

Unrelated tests fail because “no expectation defined” when using Mox library and stub_with/2 doesn’t seem to be of any help

and posted this question on SO:

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement