简体   繁体   中英

Clojure testing : testing the negation of an assertion

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM