我有一个服务调用包装器,它只在调用中将参数转发给服务。包装器的原因是我们可以使用 DI 容器注入包装器,从而模拟单元测试。
这是包装器的样子
public class WeatherChannelWrapper : IWeatherServiceWrapper
{
public GetLocalWeather(string zipcode)
{
TWCProxy.GetCurrentCondition(zipcode, DateTime.Now.ToString("MM/dd/yyyy hh:mm"));
}
}
一切正常,现在我需要吞下 TWCProxy 崩溃的异常。所以现在我的包装看起来像这样。
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);
}
}
}
我必须编写以下单元测试
- GetLocalWeather() 不会在发生内部异常时使应用程序崩溃
- GetLocalWeather() 记录异常
现在要测试这两种情况,我需要以某种方式引发崩溃;如何使用 NUnit 和 Automoq/Moq 做到这一点?