I have a service call wrapper that does nothing but forwards the parameter to a service in a call. The reason for the wrapper is so we can have the Wrapper Injected using a DI container and thus mocked for unit testing.
Here is how the wrapper looks like
public class WeatherChannelWrapper : IWeatherServiceWrapper
{
public GetLocalWeather(string zipcode)
{
TWCProxy.GetCurrentCondition(zipcode, DateTime.Now.ToString("MM/dd/yyyy hh:mm"));
}
}
Everything works fine, now I have a requirement to swallow exceptions on TWCProxy's crashes. So now my wrapper looks like this.
public class WeatherChannelWrapper : IWeatherServiceWrapper
{
private readonly IExceptionLogger exceptionLogger;
public WeatherChannelWrapper(IExceptionLogger logger)
{
this.exceptionLogger = logger;
}
public GetLocalWeather(string zipcode)
{
try
{
TWCProxy.GetCurrentCondition(zipcode, DateTime.Now.ToString("MM/dd/yyyy hh:mm"));
}
catch (Exception e)
{
exceptionLogger.Log(e);
}
}
}
I have to write the following Unit Tests
- GetLocalWeather() does not crash the application when an internal exception occurs
- GetLocalWeather() logs exceptions
Now to test both scenarios, I need to somehow induce a crash; how do I do that using NUnit and Automoq/Moq?