如何模拟Elmah?
我看到了这篇文章,但我对此感到有点困惑。
我正在查看选项 2
Create a wrapper class around the call to Raise and just mock out the wrapper class.
public class ErrorSignaler {
public virtual void SignalFromCurrentContext(Exception e) {
if (HttpContext.Current != null)
Elmah.ErrorSignal.FromCurrentContext().Raise(e);
}
}
但我有点困惑,因为这似乎没有实现接口,而且我不太确定为什么它似乎适合某种继承。
谢谢
I seen this post but I am sort of confused by it.
How can I mock Elmah's ErrorSignal routine?
I am looking at option 2
Create a wrapper class around the call to Raise and just mock out the wrapper class.
public class ErrorSignaler {
public virtual void SignalFromCurrentContext(Exception e) {
if (HttpContext.Current != null)
Elmah.ErrorSignal.FromCurrentContext().Raise(e);
}
}
I am kinda confused though by the fact that this does not seem to implement an interface and I am not really sure why it seems to be in place for some sort of inheritance.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的想法是,您可以在代码中使用
ErrorSignaler
类来发出错误信号,而不是直接调用 Elmah。在单元测试中运行代码时,由于HttpContext.Current
为 null,因此不会使用 Elmah 组件,并且不会出现任何空引用异常。您还可以创建一个 ErrorSignaler 接口:
这样,如果需要,可以在单元测试中模拟用于实现 IErrorSignaler 的实现。
The idea here is that you would use the
ErrorSignaler
class in your code to signal an error instead of invoking Elmah directly. When running your code in unit tests, becauseHttpContext.Current
is null, the Elmah component won't be used, and there won't be any null reference exceptions.You could also create an
ErrorSignaler
interface:That way the implementation used to implement
IErrorSignaler
can be mocked in the unit tests if required.