【发布时间】:2016-11-30 19:41:19
【问题描述】:
我有一个函数定义为:
(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吗?尝试将其更改为{:pre [(contains? m key)]} -
@OlegTheCat 很好发现!我从那里使用这个技巧:stackoverflow.com/a/24836592/1327651 我想我必须在测试和有用的 REPL 错误之间做出选择。
-
从 Clojure 1.9 开始,您不必选择。见stackoverflow.com/questions/37885542/…
标签: clojure clojure.test