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
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM.
...
New
Hello everybody :wave:
Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps:
bureaucrat - which contains a bunch ...
New
Raxx is an alternative to Plug and is inspired by projects such as Rack(Ruby) and Ring(Clojure).
1.0-rc.1 is now available. To use it re...
New
parameterized pipe in elixir: |n>
edit: negative index in |n> and mixed usage with |> are supported
example:
use ParamPipe
...
New
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New
Hello everyone,
We have just released NimbleCSV which is a small and fast CSV parsing library for Elixir. It allows developers to define...
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
Hey everyone :waving_hand:
Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-s...
New
Other popular topics
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
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
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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








