Fl4m3Ph03n1x

Fl4m3Ph03n1x

Completely replace a dependency with a mock

Background

I have an app called :my_app that has a dependency called :my_dep. :my_dep connects to the internet and downloads a bunch of files. Every time I run :my_app, it executes :my_dep because :my_dep is a runtime dependency. So, when I run tests, launch the app in my DEV environment or simply run it, :my_dep always connects to the internet to download files.

If :my_dep cannot connect, it fails, and because it is a runtime dependency, :my_app fails to launch.

My Approach

The current system means I can’t run local tests nor do anything without a connection to the internet. So my attempt at fixing this was to follow the approach suggested by mocks and explicit contracts and to create an interface (add a boundary between :my_app and :my_dep) and to create a mock that implements the interface.

Following is an example of how I am implementing this technique:

Geolocation Module of MyApp:

defmodule MyApp.Geolocation do

  @geo Application.get_env(:my_app, :my_dep_api)

  require Logger

  @spec geolocate(String.t) :: String.t
  def geolocate(ip) do
    ip
    |> @geo.find()
    |> get_country(ip)
  end

  @spec get_country({:ok, [String.t]}, String.t) :: String.t
  defp get_country({:ok, [country | _tail]}, _ip), do: String.upcase(country)
end

DEV config file of MyApp:

config :my_app,
  my_dep_api: MyDep.StaticMock

The StaticMock file:

defmodule MyDep.StaticMock do
  @behaviour MyApp.IMyDep

  @behaviour MyApp.IMyDep
  @spec find(String.t) :: {:ok, [String.t]}
  def find(_ip), do: {:ok, ["ES", "cat", "b"]}
end

Problem

The issue here, is that when I launch my app in DEV, it still runs the original my_dep app and it still tries to connect and download the files.

Question

How can I fix this?

Marked As Solved

al2o3cr

al2o3cr

Apologies for the confusion, I totally skipped this part of your question:

The right place to fix this isn’t in MyApp.Geolocation, but rather where :my_dep is being started: might be in :my_app’s application.exs or in mix.exs.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement