[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.