2

我有一个函数定义为:

(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))

需要什么来检查断言是否会引发错误?

4

1 回答 1

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
于 2016-11-30T21:43:33.110 回答