Clojure测试:测试断言的否定 [英] Clojure testing : testing the negation of an assertion

查看:105
本文介绍了Clojure测试:测试断言的否定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这适用于我:

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

但这样爆炸了

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

以下错误:

  java.lang.RuntimeException:无法解析symbol:thrown?在这种情况下

大概会抛出?在is里面有一些特殊的含义,当我把not放在路上时是不可见的。



那么如何测试一个断言的否定

解决方案

您是否正确,抛出?必须在之后出现的特殊声明。如果你想测试一个异常没有被抛出(换句话说,调用评价为某些东西),你可能想做一些像:



<$ c $虽然有一个问题:如果结果是falsey(false,false),它将会失败,但是c>(is(.fun obj 1))



<零)。要完全确定:

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

(这是一个重言式,只有当它抛出异常时才会失败。 / p>

This works for me :

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

But this blows up

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

with the following error :

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

Presumably "thrown?" has some special meaning inside the "is" and isn't visible when I put the "not" in the way.

So how can I test a negation of an assertion like thrown?

解决方案

You're correct, thrown? is a special assertion that must appear after is. If you want to test that an exception is not thrown (in other words, the call evaluated to something), you might want to do something like:

(is (.fun obj 1))

although that has one problem: it would fail if the result is falsey (false, nil). To be completely sure:

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

(this is a tautology, and will only fail if it does throw an exception).

这篇关于Clojure测试:测试断言的否定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆