wceolin

wceolin

Mocking a module called from FLAME.call

I’m using Mox to mock some modules in my app. Usually, it works fine but I have a function that I’m calling inside FLAME.call/2 and it broke my mock:

FLAME.call(Zoonk.FLAME.ImageOptimization, fn ->
  Zoonk.Storage.optimize!(key, args["size"] || 500)
end)

After I added it to FLAME.call/2, I started getting the following error:

** (Mox.UnexpectedCallError) no expectation defined for Zoonk.Storage.StorageAPIMock.optimize!/2 in process #PID<0.5593.0> with args ["16.png", 500]
             (mox 1.1.0) lib/mox.ex:820: Mox.__dispatch__/4
             (zoonk 0.1.0) lib/storage/storage_context.ex:177: Zoonk.Storage.optimize!/2
             (flame 0.2.0) lib/flame/runner.ex:431: anonymous fn/3 in FLAME.Runner.remote_call/4

I’ve tried calling Mox.allow/3 in test_helper.ex but it didn’t work:

Mox.allow(Zoonk.Storage.StorageAPIMock, self(), fn ->
 GenServer.whereis(Zoonk.FLAME.ImageOptimization)
end)

Any ideas how to make it work inside FLAME.call/2? Thanks!

Most Liked

al2o3cr

al2o3cr

Zoonk.FLAME.ImageOptimization is the name of the FLAME.Pool process that manages runners. It receives a :checkout message and returns the actual PID of the runner, which is what you’d need to share the mock with.

al2o3cr

al2o3cr

There are three processes involved here:

  • the test process, that’s calling Mox functions and the code-under-test
  • a process running with callbacks defined in the module FLAME.Pool, named Zoonk.FLAME.ImageOptimization
  • a runner process, that ultimately tries to call Zoonk.Storage.optimize!

The Mox.allow you posted in the original post is sharing the mock with the second process, but it’s actually needed in the third.


FLAME.call “checks out” a runner and then sends it work; doing a checkout outside of that will return a different runner process from the pool, so that won’t help.


Your options, IMO, are:

  • use Mox globally (with the corresponding loss of concurrency)
  • change your architecture slightly: consider wrapping the FLAME.call in a function and using Mox to replace that. Then, test the code inside the FLAME.call separately.

Last Post!

wceolin

wceolin

I see, thanks. I thought there was a way to get the PID of that third process to use with Mox.allow.

I was trying to avoid running tests synchronously. In my initial tests, the runtime doubled from 3s to 6s, but moving those sync tests to a separate module brought it back to 3s. It seems like the best option for now.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement