这是代码的传递版本:
正常功能:通过
(defn my-fn
[]
(throw (IllegalStateException.)))
(fact
(my-fn) => (throws IllegalStateException))
这是它的宏版本:
(defmacro my-fn
[]
(throw (IllegalStateException.)))
(fact
(my-fn) => (throws IllegalStateException))
这里失败是输出:
LOAD FAILURE for example.dsl.is-test
java.lang.IllegalStateException, compiling:(example/dsl/is_test.clj:14:3)
The exception has been stored in #'*me, so `(pst *me)` will show the stack trace.
FAILURE: 1 check failed. (But 12 succeeded.)
这与我刚刚将 defn 更改为 defmacro 的代码相同。
我不明白为什么这不起作用?
最佳答案
问题是你的宏是错误的。当您的函数在运行时抛出错误时,宏会在编译时抛出错误。以下内容应该可以修复该行为:
(defmacro my-fn
[]
`(throw (IllegalStateException.)))
现在您的宏调用将被抛出的异常所取代。类似这样的事情:
(fact
(throw (IllegalStateException.)) => (throws IllegalStateException))
关于unit-testing - Midje 在 Clojure 中的宏上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40664590/