Over a million developers have joined DZone.

Clojure: expectations & with-redefs

DZone's Guide to

Clojure: expectations & with-redefs

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

In general, when I'm writing tests, the pure functions end up as bare expects and the impure functions end up as scenarios. The following contrived namespace keeps a list of users and allows you to get the full name of each user.

(ns user)

(def users (atom #{}))

(defn full-name [{:keys [fname lname]}] (str fname " " lname))

(defn all-names [] (map full-name @users))

The tests for this namespace would often look something like the following code:

(ns user-expectations
  (:use expectations user))

(expect "John Dory" (full-name {:fname "John" :lname "Dory"}))

(ns user-expectations.scenarios
  (:use expectations.scenarios user))

  (with-redefs [users (atom #{{:fname "Mary", :lname "Dory"} {:fname "John", :lname "Dory"}})]
    (expect ["Mary Dory" "John Dory"] (all-names))))

It feels natural to put the with-redefs in a scenario, since scenarios also support (and often make use of) stubbing, localize-state, & freeze-time. However, there's really no reason that you need to use a scenario if you're simply looking to redef a var.

The following test provides the same level of functionality verification, without needing to use expectations.scenarios: 

(ns user-expectations
  (:use expectations user))

(expect "John Dory" (full-name {:fname "John" :lname "Dory"}))

(expect ["Mary Dory" "John Dory"]
  (with-redefs [users (atom #{{:fname "Mary", :lname "Dory"} {:fname "John", :lname "Dory"}})]

scenarios are great, but these days I try to keep things simple with bare expectations whenever possible.


Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.


Published at DZone with permission of Jay Fields, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}