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?

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.

1 Like