elixir - How to stub (or prevent running) of a call to a worker in my ExUnit test? -
i have phoenix app (which restful api no front end) , 1 of controllers stuff want test, @ end of controller calls dispatcher sends payload off worker (run under poolboy) process received payload in background.
in controller test, don't want test stuff worker doing, want know dispatcher worker has been called correct payload (e.g. calledwith() )
and ideally dispatcher function stubbed, actual thing never ran.
i pass additional parameter dispatcher ensure never runs code on worker, seems messy, whereas stub seems idea.
thanks
edit
dispatcher code:
defmodule convert.dispatcher def dispatch(data) fo spawn (fn() -> parallel(data) end) end def parallel(data) #pass off poolboy end end test mock:
with_mock convert.dispatcher, [dispatch: fn(_opts) -> :ok end] response = conn(:post, "/", data) |> send_request body = response.resp_body |> poison.decode! assert response.status == 201 assert called convert.dispatcher.dispatch("") end
there mocking library called "mock" can use temporarily mock modules in tests. example:
defmodule mycontrollertest use exunit.case, async: false use routerhelper import mock setup logger.disable(self()) :ok end test "dispatches worker" with_mock mydispatcher, [dispatch: fn(_opts) -> :ok end] call(router, :get, "/my/route") assert called mydispatcher.dispatch(foo: "bar") end end end
Comments
Post a Comment