NUnit 和 MOQ:测试捕获异常时调用另一个方法的 try catch
NUnit & MOQ: Testing a try catch that calls another method when an Exception is caught
我知道 MOQ 框架并不是真正为在这种情况下提供帮助而设计的,但也许您可以提供帮助...
我有一个使用 try/catch 的方法,每当抛出异常时调用通知方法。我想要做的是创建一个 integration/unit 测试来检查以确保在抛出任何异常时调用 SendNotification。
正在测试的方法:
public virtual void MonitorIntradayBuilds(IIntradayBuilds intradayBuilds)
{
try
{
var intradayBuildFound = intradayBuilds.CheckForIntradayBuilds();
if (intradayBuildFound && !IntradayBuildsComplete && !DailyBuildsFound)
{
IntradayBuildsComplete = intradayBuilds.StartIntradayBuilds();
//should start daily builds?
}
}
catch (Exception ex)
{
SendNotification("MonitorIntradayBuilds threw an exception", ex);
}
}
测试用例:
[Test]
public void it_should_notify_developers_immediately_if_there_is_a_problem_when_checking_for_intraday_builds()
{
//Arrange
var mockDua = new Mock<DUA>();
var mockIB = new Mock<IIntradayBuilds>();
//Act
mockIB.Setup(x => x.CheckForIntradayBuilds()).Throws(new Exception());
mockDua.Object.MonitorIntradayBuilds(mockIB.Object);
//Assert
mockDua.Verify(x => x.SendNotification(It.IsAny<string>(), It.IsAny<Exception>()), Times.Once);
}
我一直点击 Moq.MockException 然后看到 SendNotification "expected an invocation on the mock once, but was 0 times..."
我试过在测试用例上使用 [ExpectedException] 属性,但无济于事。它使测试通过,但仍未调用 SendNotification 方法。
有什么想法吗?
解决了。
原来你需要在你正在模拟的被测系统上设置 CallBase 属性。
测试用例现在是:
[Test]
public void it_should_notify_developers_immediately_if_there_is_a_problem_when_checking_for_intraday_builds()
{
//Arrange
var mockDua = new Mock<DUA>();
var mockIB = new Mock<IIntradayBuilds>();
mockDua.CallBase = true; // <<<< Added this line!
//Act
mockIB.Setup(x => x.CheckForIntradayBuilds()).Throws(new Exception());
mockDua.Object.MonitorIntradayBuilds(mockIB.Object);
//Assert
mockDua.Verify(x => x.SendNotification(It.IsAny<string>(), It.IsAny<Exception>()), Times.Once);
}
希望对其他人有所帮助 :)
我知道 MOQ 框架并不是真正为在这种情况下提供帮助而设计的,但也许您可以提供帮助...
我有一个使用 try/catch 的方法,每当抛出异常时调用通知方法。我想要做的是创建一个 integration/unit 测试来检查以确保在抛出任何异常时调用 SendNotification。
正在测试的方法:
public virtual void MonitorIntradayBuilds(IIntradayBuilds intradayBuilds)
{
try
{
var intradayBuildFound = intradayBuilds.CheckForIntradayBuilds();
if (intradayBuildFound && !IntradayBuildsComplete && !DailyBuildsFound)
{
IntradayBuildsComplete = intradayBuilds.StartIntradayBuilds();
//should start daily builds?
}
}
catch (Exception ex)
{
SendNotification("MonitorIntradayBuilds threw an exception", ex);
}
}
测试用例:
[Test]
public void it_should_notify_developers_immediately_if_there_is_a_problem_when_checking_for_intraday_builds()
{
//Arrange
var mockDua = new Mock<DUA>();
var mockIB = new Mock<IIntradayBuilds>();
//Act
mockIB.Setup(x => x.CheckForIntradayBuilds()).Throws(new Exception());
mockDua.Object.MonitorIntradayBuilds(mockIB.Object);
//Assert
mockDua.Verify(x => x.SendNotification(It.IsAny<string>(), It.IsAny<Exception>()), Times.Once);
}
我一直点击 Moq.MockException 然后看到 SendNotification "expected an invocation on the mock once, but was 0 times..."
我试过在测试用例上使用 [ExpectedException] 属性,但无济于事。它使测试通过,但仍未调用 SendNotification 方法。
有什么想法吗?
解决了。
原来你需要在你正在模拟的被测系统上设置 CallBase 属性。
测试用例现在是:
[Test]
public void it_should_notify_developers_immediately_if_there_is_a_problem_when_checking_for_intraday_builds()
{
//Arrange
var mockDua = new Mock<DUA>();
var mockIB = new Mock<IIntradayBuilds>();
mockDua.CallBase = true; // <<<< Added this line!
//Act
mockIB.Setup(x => x.CheckForIntradayBuilds()).Throws(new Exception());
mockDua.Object.MonitorIntradayBuilds(mockIB.Object);
//Assert
mockDua.Verify(x => x.SendNotification(It.IsAny<string>(), It.IsAny<Exception>()), Times.Once);
}
希望对其他人有所帮助 :)