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

lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New

We're in Beta

About us Mission Statement