Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 658
Snehaa Ganesan Avatar asked Mar 14 '19 20:03

Snehaa Ganesan


1 Answers

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)))
like image 131
Carcigenicate Avatar answered Nov 18 '22 10:11

Carcigenicate