yeroc

yeroc

Mox Switch Context / Abstraction Layers

Hi all! Deleted the previous post, formatting was very messed up. Apologies!

I’ve got some Mox tests working with some API functions, but the current way I have it organized doesn’t seem quite right to me. Hoping for some guidance and to understand better.

Current module context-

api_calls.ex houses all of the direct interaction with urls and decoding the whole of JSON responses, i.e.

  defp handle_response(url, headers) do
    case http_client().get(url, headers) do
      {:ok, %{body: body}} ->
        Jason.decode!(body)

      {:error, reason} ->
        {:error, reason}
    end
  end

  defp http_client, do: Application.get_env(:my_app, :http_client)

fetch_users.ex are the individual functions that call the API/url functions and provide specific transformations of the returned JSON data, clean it up, maps etc. This is where I’m noting the behaviour for Mox to mock. A separate behaviour.ex file has the callbacks to all the functions here i.e.


@behaviour FetchUsersBehaviour

def get_ids(names, location) do
    names
    |> Enum.map(&Task.async(fn -> ApiCalls.fetch_id(&1, location) end))
    |> Enum.map(&Task.await(&1))
    |> Enum.map(& &1["ids"])
  end

fetch_users_switch.ex calls all the functions in fetch_users.ex, but with the impl layer that Mox uses. i.e.

 def get_ids(names, location) do
    fetch_impl().get_ids(names, location)
  end

defp fetch_impl() do
    Application.get_env(:my_app, :users, FetchUsers)
  end

fetcher.ex is the main module that chains/pipes together all the individual functions of fetch_users.ex, but does so by going through fetch_users_switch.ex.

alias FetchUsersSwitch, as: Fetch

def do_all(name, location) do
name
      |> Fetch.get_ids(location)
      |> Fetch.foo(location)
      |> Fetch.bar(location)
      |> Fetch.more(location)
end

My question might be more about subjectivity with how things should be organized, but am I doing this correctly with integrating Mox? Is there a simpler way to do this and Ive just overthought it?

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
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
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

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
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
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
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