我的函数定义为:
(defn strict-get
[m key]
{:pre [(is (contains? m key))]}
(get m key))
然后我对它进行了测试:
(is (thrown? java.lang.AssertionError (strict-get {} :abc)))
但是这个测试失败了:
;; FAIL in () (myfile.clj:189)
;; throws exception when key is not present
;; expected: (contains? m key)
;; actual: (not (contains? {} :abc))
检查断言是否会引发错误需要什么?
答案 0 :(得分:3)
您的断言因为嵌套两个is
而失败的原因。内部is
已捕获异常,因此外部is
测试失败,因为没有任何内容被抛出。
(defn strict-get
[m key]
{:pre [(contains? m key)]} ;; <-- fix
(get m key))
(is (thrown? java.lang.AssertionError (strict-get {} nil)))
;; does not throw, but returns exception object for reasons idk
(deftest strict-get-test
(is (thrown? java.lang.AssertionError (strict-get {} nil))))
(strict-get-test) ;; passes