Jay Fields is a software developer at DRW Trading. He has a passion for discovering and maturing innovative solutions. His most recent work has been in the Domain Specific Language space where he's delivered applications that empowered subject matter experts to write the business rules of the applications. He is also very interested in maturing software design through software testing. Jay is a DZone MVB and is not an employee of DZone and has posted 115 posts at DZone. You can read more from them at their website. View Full User Profile

Clojure: Use expect-let To Share A Value Between expected And actual

11.11.2012
| 1794 views |
  • submit to reddit

 Most of the time you can easily divorce the values needed in an expected form and an actual form of an expectation. In those cases, nothing needs to be shared and your test can use a simple bare expect. However, there are times when you need the same value in both the expected and actual forms - and a bare expect doesn't easily provide with a way to accomplish that.

In version 1.4.16 or higher of expectations, you can now use the expect-let macro to let one or more values and reference them in both the expected and actual forms.

Below is a simple example that makes use of expect-let to compare two maps that both have a DateTime.

(ns blog-expectations
  (:use expectations)
  (:import [org.joda.time DateTime]))

(defn timestamp-event [m t]
  (assoc m :time t))

(expect-let [now (DateTime.)]
            {:time now}
            (timestamp-event {} now))

If possible you should prefer expect, but expect-let gives you another option for the rare cases where you absolutely need to share a value.


Published at DZone with permission of Jay Fields, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)