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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement