Stubs in Speclj
2023-06-09
When testing the user interface of a program, one can use stubs to redefine user input to be predetermined.
For testing in Clojure, I use Speclj which allows me to create and test stubs like so:
Add (with-stubs)
before using stubs to make sure stub recoding is bound.
(with-stubs)
Then use (with-redefs)
to redefine a function to evaluate to whatever you want to simulate the user
input to be.
(with-redefs [read-line (stub :read-line {:return "1"})]
(should= "1" (read-line))
(should-have-invoked :read-line))
In this case, I redefine the read-line
function to evaluate to "1".
Notice that after the test, I add (should-have-invoked :read-line)
, which just tests if the
stub actually worked.