Sne*_*san 5 unit-testing functional-programming clojure clojure-testing
我正在为我的第一个Clojure项目编写测试用例。在这里,如果“:meat”的值为空,我希望测试失败:
(deftest order-sandwich
(let [response {:meat "" :bread "yes" :add-on "lettuce"}]
(is (= (:bread response) "yes"))
(is (not (nil? (:meat response))))))
Run Code Online (Sandbox Code Playgroud)
但是我的测试运行成功(返回“ nil”)。
有人知道为什么会这样吗?有一个更好的方法吗?
我提前谢谢你!!
An empty String is not nil:
(nil? "")
=> false
Run Code Online (Sandbox Code Playgroud)
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)))
Run Code Online (Sandbox Code Playgroud)