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
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
source share