异常如何突破try\catch块?
摘自《单元测试的艺术》一书:
在Rhino Mocks中,严格的mock是通过调用StrictMock创建的 方法。意外的方法调用异常总会被抛出, 即使您的测试包含您认为的全局 try-catch 子句 会捕获从隔离框架抛出的此类异常。
那么我到底如何在自己的程序中实现这种行为呢?
A citation from "The art of unit testing" book:
In Rhino Mocks, strict mocks are created by calling the StrictMock
method. Unexpected method call exceptions will always be thrown,
even if your test contains a global try-catch clause, which you’d think
would catch such an exception thrown from the isolation framework.
So how exactly can I implement this behaviour in the program of my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的应用程序中永远不应该出现这种类型的场景,因为您不应该捕获全局异常。您应该只捕获要处理的异常类型,其他所有内容都应该允许冒泡。
You should never really have this type of scenario in your application as you shouldn't be catching global exceptions. You should only catch the type of exceptions you are going to handle, everything else should be allowed to bubble up.
一种方法可能是实现拦截框架,例如Unity.Interception(也存在其他框架,例如来自Castle)。
使用Unity.Interception你可以编写一个
ICallHandler
的实现,它有一个Invoke
方法,你可以在其中捕获和重新抛出异常:当然深入研究Rhino的源代码.Mocks 将是最直接的方法。至少,使用 Reflector 并浏览 dll。
One way might be to implement an interception framework, such as
Unity.Interception
(others also exists, e.g. from Castle).Using Unity.Interception you can write an implementation of
ICallHandler
, which has anInvoke
method where you can trap and re-throw exceptions:Of course delving into the source code of Rhino.Mocks would be the most direct way to do this. At the very least, use Reflector and browse the dll.