xgeek116

xgeek116

Mox is ignoring my Mocked module and calling the real function

I’m using using mox to help me with my unit testing (for now, I want to mock a function that fetches external API).

my mix.exs :
{:mox, "~> 1.0", only: :test}

My test_helper.exs :

ExUnit.start()

Mox.defmock(MyApp.MockExternalSources, for: MyApp.MyModule.ExternalSources)

My module code (MyApp.MyModule.ExternalSources) :

defmodule MyApp.MyModule.ExternalSources do

    @callback http_get_resource(url :: String.t(), headers :: list()) :: {:ok, map()} | :error
    @callback fetch_one_external_resource(params :: map()) :: map()
  
    require Logger
  
    def fetch_one_external_resource(params) do
      with {:ok, data} <-
        http_get_resource(params["url"], params["headers"]) do
        do_something(data)
      else
        :error -> %{}
      end
    end
  
    def http_get_resource(url, headers) do
      case HTTPoison.get(url, headers) do
        {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
          {:ok, Poison.decode!(body)}
  
        {:ok, %HTTPoison.Response{status_code: 404}} ->
          :error
  
        {:error, %HTTPoison.Error{reason: _reason}} ->
          :error
      end
    end
  end

And my test module :

defmodule MyApp.MyModule.ExternalSources.Test do
  use ExUnit.Case, async: true
  import Mox

  setup :verify_on_exit!

  test "fetch and process external data" do

    MyApp.MockExternalSources
    |> expect(:http_get_resource, fn _url, _headers ->
      {:ok, %{status_code: 200, body: %{key1: "value1", key2: "value2"}}}
    end)

    params = %{
      "url" => "xxx",
      "headers" => "yyy"
    }

    assert MyApp.MyModule.ExternalSources.fetch_one_external_resource(params) ==
             %{"new_key1" => "value1", "new_key2" => "value2"}
  end
end

But I always get a failed assertion and the behaviour is that the right value (MyApp.MyModule.ExternalSources.fetch_one_external_resource(params)) is actually calling the external API and getting the real values ignoring my static data in the mock (%{key1: "value1", key2: "value2"})

Any help please !

Most Liked

dorgan

dorgan

Or you could just use Patch :wink:

Where Next?

Popular in Questions Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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...
3268 119930 1237
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement