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

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement