nmacuk

nmacuk

Elixir Mock Module called from other Worker

Hello,
I have a worker that calls another module

def perform(%Oban.Job{args: form_response}) do
    sms_typeform_request_uuid = get_in(form_response, ["hidden", "sms_typeform_request_uuid"])

    response =
      case get_in(form_response, ["answers", Access.at(0), "choices", "labels"]) do
        ["Yes" <> _] -> "confirmed"
        ["No" <> _] -> "declined"
      end

    message =
      MyApp.Client.publish_message(
        name: "typeform_response",
        correlation_key: sms_typeform_request_uuid,
        variables: %{sms_typeform_response: response}
      )

    case message do
      :ok -> :ok
      {:error, error} -> {:error, error}
    end
end

And in my test I have a following setup

test_with_mock "job", MyApp.Client,
    [publish_message: fn(_) ->
      :ok
    end]do
    assert :ok ==
             perform_job(MyApp.Worker.TypeformResponseNotifier, %{
               "answers" => [
                 %{
                   "choices" => %{"labels" => ["Yes, Date is ok"]},
               ],
               "hidden" => %{"sms_typeform_request_uuid" => "UYl2Mw_m"}
             })

    assert_called(
      MyApp.Client.publish_message(
        task_type: "typeform_response",
        correlation_key: UYl2Mw_m,
        variables: %{sms_typeform_response: "confirmed"}
      )
    )
  end```

But the test fails with this:

Expected call but did not receive it. Calls which were received:

 0. Elixir.MyApp.Client.publish_message([name: "typeform_response", correlation_key: "UYl2Mw_m", variables: %{sms_typeform_response: "confirmed"}]) (returned :ok)
 code: assert_called(

What Am I doing wrong? What happens?

Most Liked

stefanchrobot

stefanchrobot

You’re passing "typeform_response" under a different key in your tests vs in the code (task_type vs name).

If anyone’s wondering, the test is using Mock.

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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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