断言抛出的 clojure 测试
clojure test that assertion throws
我有一个函数定义为:
(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))
需要什么来检查断言是否会引发错误?
您的断言失败的原因是您嵌套了两个 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
我有一个函数定义为:
(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))
需要什么来检查断言是否会引发错误?
您的断言失败的原因是您嵌套了两个 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