5

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

Snehaa Ganesan
  • 509
  • 3
  • 10

1 Answers1

6

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)))
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117