I'm querying a third-party SDK with a fully async-await method and SDK returns an AggregateException
in a normal scenario. In AggregateException
inner exception will be a type of ABCException
.
But while mocking the same code in unit test cases with Moq and XUnit it will return ABCException
directly.
This is the code part for the catch block:
public async Task<Response> ExecuteAsync(IFunction function)
{
try
{
return await InvockAsync(function, location);
}
catch (AggregateException ex)
{
ex.Handle(e =>
{
if (e is ABCException)
{
DoSomething();
}
}
}
catch (ABCException ex)
{
DoSomething();
}
}
This is the code block for the unit test case:
public async Task UnitTest()
{
// Arrange
var mockService = new Mock<IService>();
mockService.Setup(x => x.InvockAsync(It.IsAny<IFunction>())).ReturnsAsync(new Response());
var instance = new OtherService(mockService.Object);
Func<Task> func = async () => await instance.ExecuteAsync(data);
//Act
var data = await Assert.ThrowsAsync<AggregateException>(func);
//Assert
Assert.IsType<AggregateException>(data);
}
In the above code, a normal call comes AggregateException
and unit test cases call comes into ABCException
.
question from:
https://stackoverflow.com/questions/66058106/aggregate-exception-not-caught-in-xunit-test-cases 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…