【发布时间】:2015-11-10 08:42:37
【问题描述】:
我想对我的代码的异常处理部分进行单元测试。为了做到这一点,我使用with-redefs 重新绑定可以抛出异常的API,以便在测试期间抛出异常。我的测试函数看起来像这样
(deftest exception-handling
(testing "Map exception to Ring-response map"
(with-redefs [clj-http/get
(constantly (throw (ex-info "unexpected error" {:cause "A terrible calamity"})))]
(is (= 500
(:status
(some-function-calling-http-get arg))))
)
)
)
运行lein test 会导致消息出错:
ERROR in (exception-handling) (core.clj:4593)
Uncaught exception, not in assertion.
expected: nil
actual: clojure.lang.ExceptionInfo: unexpected error
at clojure.core$ex_info.invoke (core.clj:4593)
在with-redefs 中使用(constantly (throw... 或仅断言使用thrown? 引发异常也会导致相同的错误。
基本上我在寻找constantly 的宏版本。
【问题讨论】:
标签: unit-testing clojure