Search code examples
unit-testingclojurefunctional-programmingclojure-testing

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


Solution

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