我看过这篇文章,但我有点困惑。
How can I mock Elmah's ErrorSignal routine?
我正在看选项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);
}
}
我有点困惑,虽然这似乎没有实现一个接口,我不确定为什么它似乎适用于某种继承。
由于
答案 0 :(得分:2)
这里的想法是,您将在代码中使用ErrorSignaler
类来表示错误,而不是直接调用Elmah。在单元测试中运行代码时,因为HttpContext.Current
为null,所以不会使用Elmah组件,也不会有任何空引用异常。
您还可以创建ErrorSignaler
界面:
public interface IErrorSignaler
{
void SignalFromCurrentContext(Exception e);
}
这样,如果需要,可以在单元测试中模拟用于实现IErrorSignaler
的实现。