使用 Stub 抛出 ExpectedException TimeoutException 不工作 c#
Throw ExpectedException TimeoutException with Stub is not working c#
我有这个单元测试,我一直试图在它上面抛出异常,但我做不到,你能帮帮我吗?
[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);
}
单元测试不等待异步方法。没有人要求异步方法的结果。您需要对其执行 .Wait 以强制其等待结果。
[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
commandHandler.HandleAsync(deleteModel, token).Wait();
}
我有这个单元测试,我一直试图在它上面抛出异常,但我做不到,你能帮帮我吗?
[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);
}
单元测试不等待异步方法。没有人要求异步方法的结果。您需要对其执行 .Wait 以强制其等待结果。
[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
commandHandler.HandleAsync(deleteModel, token).Wait();
}