Fl4m3Ph03n1x
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
Application.get_env(:my_app, :my_dep_api)vsconfig :bidtor, my_dep_api: MyDep.StaticMockIs this just a copy/paste error?
Fl4m3Ph03n1x
Yes. Fixed ! (thanks)
al2o3cr
Have you tried forcing recompilation? IIRC the module attribute is read from the application env at compile-time and stored in the BEAM file.
Fl4m3Ph03n1x
I have deleted the
_buildanddepsfolders and forced recompilation. The issue still persists.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_depis being started: might be in:my_app’sapplication.exsor inmix.exs.