Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

unit testing - Aggregate Exception not caught in xunit test cases

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...