How can I mock Elmah's ErrorSignal routine?

陌路散爱 提交于 2019-11-30 06:36:58
Matthew Manela

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

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

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

  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);
        } 
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!