[英]clojure test that assertion throws
I have a function defined as: 我有一个函数定义为:
(defn strict-get
[m key]
{:pre [(is (contains? m key))]}
(get m key))
And then I have a test for it: 然后我对此进行了测试:
(is (thrown? java.lang.AssertionError (strict-get {} :abc)))
However this test fails: 但是此测试失败:
;; FAIL in () (myfile.clj:189)
;; throws exception when key is not present
;; expected: (contains? m key)
;; actual: (not (contains? {} :abc))
What is needed to check that the assertion would throw an error? 检查断言是否会引发错误需要什么?
The reason your assertion fails because you are nesting two is
. 由于嵌套两个而导致断言失败的原因
is
。 The inner is
already catches the exception so the outer is
test then fails because nothing is thrown. 内
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.