Midje is an alternative testing framework for Clojure. It is easy to use and tests developed with Midje are well readable. This talk will give a short introduction to Midje and show you how to compose facts, checkers, checkables and prerequisites into an effective test suite for a Clojure program.
16. Can be used instead of values on the right side
of a checkable
Checkers
17. (facts "about my list"
(fact "it contains the value 5"
'(1 2 3 4 5 6) => (contains 5))
(fact "it contains the values 2 and 3"
'(1 2 3 4 5 6) => (contains 2 3))
(fact "it contains an odd number followed by
an even number"
'(1 2 3 4 5 6) => (contains odd? even?)))
Checkers - contains
18. (facts "about my map"
(fact "the value of x is 1"
{:x 1 :y 2} => (contains {:x 1}))
(fact "the value of y is even"
{:x 1 :y 2} => (contains {:y even?})))
Checkers - contains
23. (defn find-user [username]
;; Code that finds the user in the database
)
(defn authenticate [username password]
(let [user (find-user username)]
(if (= (:password user) password)
user)))
(fact "it authenticates a user with a valid password"
(authenticate "the fly" "12345") => truthy
(provided
(find-user "the fly") =>
{:name "the fly" :password "12345"}))
Prerequisites - Stub
24. (fact "it calls the second function 5 times with argument
'foo'"
(first-function "foo") => irrelevant
(provided
(second-function "foo") => irrelevant :times 5))
(fact "it never calls the third function with any
argument"
(first-function "foo") => irrelevant
(provided
(third-function anything) => irrelevant :times 0))
Prerequisites - Mock
Fortunately, you only have to replace one letter to improve things a lot. ;)
My name is Tobias Bayer…
I work as a developer for inovex
inovex: medium sized Software Company from Karlsruhe with offices in Cologne and Munich (where I work)
By the way, I’ll push the sample code I’ll be showing here to GitHub after the talk...
These are some of the Midje examples I’ll walk you through in the talk…
You can see some of the major building blocks of a Midje test…
Name some of the building blocks:
“facts”
“=> arrow”
“provided”
live demo afterwards
But, first things first.
Why should you use Midje for testing Clojure apps?
Or why do I prefer to use Midje?
“non-lispy” at a Clojure conference
more readable in tests, first execution _then_ expectation, grew up reading from left to right
i also think it was done by mistake in JUnit when they put the expectation before the execution in the assert-methods :)
A checkable is the heart of a fact
Here you execute your function under test with its arguments and define your expectation
Consists of three parts: left hand side, right hand side and arrow
Function under test, arrow, expectation
=not=> arrow
Other arrows available
ca. at 10 minutes
anything/irrelevant -> prerequisites, side effects
truthy/falsey -> nil/false not nil/true