Double - A library for creating injectable stubs

I’ve been working on this library for a bit and we’re using it successfully at my day job. It makes creating stub modules for testing much easier and does NOT override the behavior of existing modules so it’s async-friendly. I hope it helps others out, and I also welcome any feedback.

stub = double(File) 
|> allow(:read, fn("fileA.txt") -> {:ok, "file A contents"} end)
|> allow(:read, fn("fileB.txt") -> {:ok, "file B contents"} end)
stub.read("fileA.txt") # {:ok, "file A contents"})
stub.read("fileB.txt") # {:ok, "file B contents"})
assert_receive({:read, "fileA.txt"})
assert_receive({:read, "fileB.txt"})

Also, I made a fun video about unit testing and why Double is useful here: https://youtu.be/cyU_SFyVRro

3 Likes