Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

 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.


Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}