tags:

views:

53

answers:

1

Hi

I seen this post but I am sort of confused by it.

http://stackoverflow.com/questions/1019833/how-can-i-mock-elmahs-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

+2  A: 

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, because HttpContext.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:

public interface IErrorSignaler
{
    void SignalFromCurrentContext(Exception e);
}

That way the implementation used to implement IErrorSignaler can be mocked in the unit tests if required.

Nader Shirazie
Ya that's what I ended up doing then I mocked it. I just was wondering the guy had "Virtual"
chobo2
That would allow him to subclass the ErrorSignaler to add even more behaviour to the `Signal` method. That flexibility could be useful in some situtations.
Nader Shirazie