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

New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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

We're in Beta

About us Mission Statement