我怎么能嘲笑ELMAH的日常ErrorSignal? [英] How can I mock Elmah's ErrorSignal routine?

查看:305
本文介绍了我怎么能嘲笑ELMAH的日常ErrorSignal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用ELMAH来处理我们的ASP.Net MVC C#应用程序,并在我们捕获的异常错误,我们正在做这样的事情:

We're using ELMAH for handling errors in our ASP.Net MVC c# application and in our caught exceptions, we're doing something like this:

ErrorSignal.FromCurrentContext().Raise(exception);

但是当我试图单元测试捕获的异常,我得到这个消息:

but when I try to unit test the caught exceptions, I get this message:

System.ArgumentNullException: Value cannot be null.
Parameter name: context

我怎么能嘲笑FromCurrentContext()调用?
是否有别的我应该做的呢?事情

How can I mock the FromCurrentContext() call? Is there something else I should be doing instead?

FYI ...我们目前正在使用的起订量和RhinoMocks的。

FYI... We're currently using Moq and RhinoMocks.

谢谢!

推荐答案

由于 FromCurrentContext()的方法是,你不能简单地模拟调用它的静态方法。你有其他两个选项。

Since the FromCurrentContext() method is a static method you can't simply mock the call to it. You do have two other options.


  1. 由于 FromCurrentContext()内部使得以 HttpContext.Current 呼叫你可以把假的背景在那里面。例如:

  1. Since FromCurrentContext() internally makes a call to HttpContext.Current you can push a fake context in that. For example:

SimpleWorkerRequest request = new SimpleWorkerRequest(
    "/blah", @"c:\inetpub\wwwroot\blah", "blah.html", null, new StringWriter());

HttpContext.Current= new HttpContext(request);

通过这一点,不应该抛出异常了,因为 HttpContext.Current 不为null。

With this it should not throw the exception anymore since HttpContext.Current is not null.

创建呼叫周围的包装类,以提高,只是模拟出的包装类。

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);
    } 
}


这篇关于我怎么能嘲笑ELMAH的日常ErrorSignal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆