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

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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement