作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
情况是这样的:我正在尝试对调用函数 B 的函数 A 进行单元测试。函数 B 在弹弓 try+ 块中调用,在某些情况下,它可以使用弹弓 throw+ 抛出。我想在 midje 测试中模拟函数 B,以便它返回 try+ 块中的 catch 将实际捕获的内容。我似乎无法创造正确的东西来扔。这是代码和测试的基本简略草图:
(defn function-A
[param]
(try+
(function-B param)
(catch [:type :user-not-found]
(do-something))))
(defn function-B
[param]
(throw+ [:type :user-not-found]))
(fact "do-something is called"
(function-A "param") => (whatever is the result of calling do-something)
(provided
(function-B "param") =throws=> (clojure.lang.ExceptionInfo. "throw+: {:type :user-not-found}"
{:object {:type :user-not-found}, :environment {}}
nil)))
(try+
(try
(throw+ {:type :user-not-found})
(catch Exception e
(prn "Caught: " e)
(prn "Class: " (.getClass e))
(prn "Message: " (.getMessage e))
(prn "Cause: " (.getCause e))
(prn "Data: " (.getData e))
(throw e)))
(catch [:type :user-not-found] p
(prn "caught it")))
(try+
(try
(throw (clojure.lang.ExceptionInfo. "throw+: {:type :user-not-found}"
{:object {:type :user-not-found}, :environment {}}
nil))
(catch Exception e
(prn "Caught: " e)
(prn "Class: " (.getClass e))
(prn "Message: " (.getMessage e))
(prn "Cause: " (.getCause e))
(prn "Data: " (.getData e))
(throw e)))
(catch [:type :user-not-found] p
(prn "caught it")))
最佳答案
这是一个非常晚的响应,但是以下解决方案呢:
(defn ex+ [cause]
(try
(throw+ cause)
(catch Throwable ex
ex)))
(broken-fn) =throws=> (ex+ {:type :user-not-found})
关于unit-testing - 为什么我不能使用 midje 来模拟使用弹弓的 throw+ 抛出的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069584/
我是一名优秀的程序员,十分优秀!