msz

msz

Hammox - an automatic type checker for testing functions and mocks (based on Mox)

https://github.com/msz/hammox

I’m a big fan of rigorous unit testing. I believe most slow integrated tests can be replaced by unit tests using mocks, provided that one writes a good set of both unit and contract tests for all pieces of code. Good static type systems and checkers go a long way in ensuring contracts; unfortunately, we don’t have that luxury in Elixir.

When I found Mox I was hooked; I loved that it requires to make the contracts explicit as behaviours. However I was disappointed to find that even though it requires the user to write typespecs for the functions, those are actually never used unless you use Dialyzer. This kind of information might even be dangerous without automated checks, because when somebody forgets to update it when changing the implementations, it becomes misleading and can do more harm then good.

In Elixir, the typespecs are first class citizens and are available as code during runtime. So why not check them? That’s what I did.

The result is Hammox, an enhanced, backwards compatible version of Mox which will stop you any time you pass or return a value not conforming to typespec. In its short lifetime it already saved me from countless bugs during various refactorings of my company’s sizeable Elixir backend system.

I’m loving it, and hopefully anyone who uses Mox, or any kind of mocked unit tests, will love it too. Can I get some feedback?

Most Liked

christhekeele

christhekeele

This is great!

Verifying doubles are the best kind of mock, and marrying the best of Elixir typing (from typespecs) with the best of Elixir doubles (Mox) is a great idea!

I don’t have a good project to try this on, but I am reminded of rspec-fire, which forwarded this pattern in the Ruby testing ecosystem, was universally well-received, and was eventually entirely adopted into the parent rspec framework. Python’s mock’s auto-speccing has a similar history.

“Why not be totally changed into fire?”

Where Next?

Popular in Announcing Top

Crowdhailer
The latest release of Ace (0.10.0) includes serving content over HTTP/2. I have started writing a webserver to teach my self more about...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: GitHub - devonestes/assertions: Helpful a...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
tmbb
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
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New

Other popular topics Top

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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement