A small spying and stubbing library for Clojure and ClojureScript: test-doubles
Published by Manuel Rivero on 21/04/2018
As you may know from a previous post I’m working for GreenPowerMonitor as part of a team that is developing a challenging SPA to monitor and manage renewable energy portfolios using ClojureScript.
We were dealing with some legacy code that was effectful and needed to be tested using test doubles, so we explored some existing ClojureScript libraries but we didn't feel comfortable with them. On one hand, we found that some of them had different macros for different types of test doubles and this made tests that needed both spies and stubs become very nested. We wanted to produce tests with as little nesting as possible. On the other hand, being used to Gerard Meszaros’ vocabulary for tests doubles, we found the naming used for different types of tests doubles in some of the existing libraries a bit confusing. We wanted to stick to Gerard Meszaros’ vocabulary for tests doubles.
We started by manually creating our own spies and stubs during some time so that we could identify the different ways in which we were going to use them. After a while, my colleague André Stylianos Ramos and I wrote our own small DSL to create stubs and spies using macros to remove all that duplication and boiler plate. The result was a small library that we've been using in our ClojureScript project for nearly a year and that we've recently adapted to make it work in Clojure as well:
I’m really glad to announce that GreenPowerMonitor has open-sourced our small spying and stubbing library for Clojure and ClojureScript: test-doubles.
In the following example written in ClojureScript, we show how we are using
test-doublesto create two stubs (one with the :maps option and another with the :returns option) and a spy:
We could show you more examples here of how
test-doublescan be used and the different options it provides, but we’ve already included a lot of explained examples in its documentation.
Please do have a look and try our library. You can get its last version from Clojars. We hope it might be as useful to you as it has been for us.
Originally published in Manuel Rivero's blog.