DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Clojure: expectations & with-redefs

Clojure: expectations & with-redefs

Jay Fields user avatar by
Jay Fields
·
Jun. 16, 12 · Java Zone · Interview
Like (0)
Save
Tweet
4.42K Views

Join the DZone community and get the full member experience.

Join For Free

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))

(scenario
  (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"}})]
    (all-names)))

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

 

Clojure Testing

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Deploy Apache Kafka With Kubernetes
  • The Evolution of Configuration Management: IaC vs. GitOps
  • A Guide to Events in Vue
  • Toying With Kotlin’s Context Receivers

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo