pepicrft
Application_module - Better mocking ergonomics
Hey folks ![]()
I just shipped a new Elixir package to improve the ergonomics using mocking libraries like Mox or Hammox, application_module. I talked about the motivations in detail in this blog post, but I’ll leave you here with the TL;DR;
- Mox and Hammox propose to have a runtime function that reads the module from the application environment.
- This is common across all the modules whose tests will want to mocks some of their dependencies. This leads to a lot of duplications and potentially inconsistencies in how that information is structured inside the application environment.
- There might also be inconsistencies arising from how developers organize the behaviours and their default implementations.
I decided to eliminate all the duplications and embrace a more conventional approach enforced via macros. Here’s an example that illustrates the before and after using the package:
Before
defmodule MyModule do
@behaviour __MODULE__.Behaviour
def hello(name) do
application_env_module().hello(name)
end
def application_env_module() do
get_in(Application.get_env(:my_app, :modules), [:my_module]) || __MODULE__.Implementation
end
defmodule Implementation do
@behaviour MyModule.Behaviour
def hello(name) do
"Hello #{name}"
end
end
defmodule Behaviour do
@callback hello(name :: String.t()) :: any()
end
end
After
defmodule MyModule do
use Application.Module
defimplementation do
def hello(name) do
"Hello #{name}"
end
end
defbehaviour do
@callback hello(name :: String.t()) :: any()
end
end
Any feedback is truly appreciated. It was my first time implementing macros myself so I went through a bunch of hiccups and back-and-forths.
Popular in Announcing
In short
Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
Hey there!
I wrote a download elixir package which does exactly what its name about - an easy way to download files.
I saw solutions ab...
New
Yes, yet another parser combinator library!
Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
New
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below.
Hi everyone,
We a...
New
Another small library today.
PersistentEts
Hex: persistent_ets | Hex
GitHub: GitHub - michalmuskala/persistent_ets · GitHub
Ets table ...
New
Here is my first stab at this. README pasted below.
https://github.com/Hal9000/elixir_random
Comments and critiques are welcome.
Thank...
New
LiveMotion enables high performance animations declared on the server and run on the client.
As a follow up to my previous thread A libr...
New
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
New
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application.
This library uses Erlang esaml to provide
plug enabl...
New
Other popular topics
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
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
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
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









