msz

msz

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


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 Libraries Top

josevalim
Hi everyone, We would like to announce that Plataformatec is working on a new MySQL driver called MyXQL. Our goal is to eventually integ...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
574 16576 179
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
arkgil
Hi all! I’m happy to announce that Telemetry v0.3.0 is out! This release marks the conversion from Elixir to Erlang so that all the libr...
New
New
benlime
I created a new library GitHub - benvp/ex_cva: Class Variance Authority for Elixir which aims to make it very easy to define different va...
New
Antrater
Hi everyone! I’m thrilled to announce a huge thing. We have been developing Elixir Moon Design System for quite a while. We are finally ...
New
bryanjos
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
mattludwigs
Grizzly is a library for working with Z-Wave devices. Z-Wave is a low-frequency radio protocol for controlling smart home devices on a me...
New
bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
381 12391 119
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
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
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

Sub Categories:

We're in Beta

About us Mission Statement