How to mute (or prevent launching) a call to an employee in my ExUnit test?

I have a Phoenix application (it's just a calm api without a front end), and one of the controllers does some things that I want to test, but at the end of the controller it calls a dispatcher that sends a payload to the worker (launched under the pool) to process the received payload in the background.

In my control test, I really do not want to check what the worker is doing, I just want to know that the dispatcher for the worker was called with the correct payload (for example, calledWith ())

And ideally, the dispatcher function should be shaded, so the actual thing never starts.

I could pass an additional parameter to the dispatcher so that it never runs the worker code, but this seems very dirty, while the stub seems to be an idea.

thanks

Edit

Dispatcher Code:

defmodule Convert.Dispatcher do def dispatch(data) fo spawn (fn() -> parallel(data) end) end def parallel(data) do #pass off to poolboy end end 

Test Layout:

 with_mock Convert.Dispatcher, [dispatch: fn(_opts) -> :ok end] do response = conn(:post, "/", data) |> send_request body = response.resp_body |> Poison.decode! assert response.status == 201 assert called Convert.Dispatcher.dispatch("") end 
+6
source share
1 answer

There is a false library called "mock" that you can use to temporarily modulate modules in your tests. For instance:

 defmodule MyControllerTest do use ExUnit.Case, async: false use RouterHelper import Mock setup do Logger.disable(self()) :ok end test "dispatches a worker" do with_mock MyDispatcher, [dispatch: fn(_opts) -> :ok end] do call(Router, :get, "/my/route") assert called MyDispatcher.dispatch(foo: "bar") end end end 
+7
source

Source: https://habr.com/ru/post/989116/


All Articles