简体   繁体   中英

Clojure “is” assertion not working as expected

I am writing test cases for my first Clojure project. Here, I want the test to fail if the value of ":meat" is empty :

(deftest order-sandwich
  (let [response {:meat "" :bread "yes" :add-on "lettuce"}]
    (is (= (:bread response) "yes"))
    (is (not (nil? (:meat response))))))

But my test runs successfully (returning "nil") .

Anybody know why this happens? Is there a better way to do this?

I thank you in advance!!

An empty String is not nil :

(nil? "")
=> false

You want to test if it's empty, not nil , which can be done using seq or empty? (among other ways):

(is (not (empty? (:meat response))))
; Or use not-empty

; There's also the arguably more idiomatic way

(is (seq (:meat response)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM