【问题标题】:Clojure testing : testing the negation of an assertionClojure 测试:测试断言的否定
【发布时间】:2015-08-18 04:25:06
【问题描述】:

这对我有用:

(is  (thrown? AbstractMethodError (.fun obj 1) ))

但这会爆炸

(is (not (thrown? AbstractMethodError (.fun obj 1) )))

出现以下错误:

java.lang.RuntimeException: Unable to resolve symbol: thrown? in this context

大概是“扔了?”在“is”中有一些特殊的含义,当我把“not”放在一边时不可见。

那么我怎样才能测试一个断言的否定,比如 throw 呢?

【问题讨论】:

标签: unit-testing clojure


【解决方案1】:

你说得对,thrown?special assertion,必须出现在is 之后。如果您想测试没有抛出异常(换句话说,调用评估为某事),您可能需要执行以下操作:

(is (.fun obj 1))

虽然这有一个问题:如果结果为假(假,无),它将失败。完全确定:

(let [r (.fun obj 1)]
  (is (= r r)))

(这是一个重言式,只有在抛出异常时才会失败。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 2020-02-11
  • 2020-05-29
  • 2014-10-21
  • 1970-01-01
相关资源
最近更新 更多