我有一个我正在单元测试的类,以验证特定的异常条件是否得到妥善处理。为此,我模拟了内部调用以抛出异常的方法。
我的模拟设置如下所示:
fr.CallBase = true;
fr.Setup(m => m.PutFile(It.IsAny<IFileConnection>(), It.IsAny<string>(), It.IsAny<string>()))
.Throws(new System.IO.IOException("Test Exception", new System.Net.Sockets.SocketException()));
这正是我想要它做的。
但是,现在我想通过仅针对特定值抛出异常来测试连续性。我认为它应该是这样的:
fr.Setup(m => m.PutFile(It.IsAny<IFileConnection>(), It.Is<string>(a => a == "foo2.txt"), It.IsAny<string>()))
.Throws(new System.IO.IOException("Test Exception", new System.Net.Sockets.SocketException()));
...但这似乎不起作用。我究竟做错了什么?
根据请求,整个测试:
[Test]
public void ManualRouteInterruptedInDownloadContinuesOn()
{
var firstRoute = this.UnitOfWork.GetFirstRoute();
Route r = this.UnitOfWork.GetRouteByID(firstRoute.RouteID);
r.RegExMatch = "^foo\\d.txt$";
r.Manual = true;
r.NotifyOfNewFiles = "me@company.com";
this.UnitOfWork.Save();
var fr = new Mock<ManualRouting>(r.RouteID);
fr.CallBase = true;
fr.Setup(m => m.GetFile(It.IsAny<IFileConnection>(), It.Is<string>(a => a == "foo2.txt"), It.IsAny<string>()))
.Throws(new System.IO.IOException("Test Exception", new System.Net.Sockets.SocketException()));
fr.Object.ExecuteRoute(firstRoute.RouteID);
Assert.IsTrue(fr.Object.Errors.Count == 1);
Assert.IsTrue(fr.Object.Matches.Count == 3);
}