【发布时间】:2017-09-22 17:17:38
【问题描述】:
我有这个单元测试,我一直在尝试对其抛出异常,但我无法做到,请你帮帮我吗?
[TestMethod]
[ExpectedException(typeof(TimeoutException))]
public async Task HandleAsyncDeleteModel_WhenRepositoryFails_ThrowsException()
{
//Arrange
var token = new CancellationToken();
var deleteModel = new DeleteProcessCommand(_img, _tnt, _pro, _url);
var writeRepository = new StubIWriteRepository<Dto>()
{
DeleteIfExistsAsyncGuidGuidGuidCancellationToken = (img, tnt, pro, tkn) =>
{
throw new TimeoutException();
}
};
var Logger = new StubILogger();
var commandHandler = new CommandHandler(Logger, writeRepository, null, null, null, null, null, null);
//Act
await commandHandler.HandleAsync(deleteModel, token);
}
【问题讨论】:
-
在行为中,而不是 await do .Wait 方法。 // 执行 commandHandler.HandleAsync(deleteModel, token).Wait();试试看。
标签: c# .net unit-testing stub timeoutexception