Skip to content

ThrowExactly and ThrowExactlyAsync do not work properly with AggregateExceptions  #1045

@davidomid

Description

@davidomid

This issue has been discussed as part of #696.

This is resolved by pull request #1046

Description

The ThrowExactly and ThrowExactlyAsync methods do not work in the expected way, when AggregateExceptions are thrown.

The expectation is that asserting that a particular type of exception is thrown should not pass if that exception type is thrown within an AggregateException, instead of just throwing an exception of the expected type.

Consider the following unit test:

[Fact]
public async Task When_subject_throws_aggregate_exception_instead_of_exact_exception_it_should_throw()
{
    //-----------------------------------------------------------------------------------------------------------
    // Arrange
    //-----------------------------------------------------------------------------------------------------------
    var asyncObject = new AsyncClass();

    //-----------------------------------------------------------------------------------------------------------
    // Act
    //-----------------------------------------------------------------------------------------------------------
    Func<Task> action = () => asyncObject.ThrowAggregateExceptionAsync<ArgumentException>();
    Func<Task> testAction = () => action.Should().ThrowExactlyAsync<ArgumentException>("ABCDE");

    //-----------------------------------------------------------------------------------------------------------
    // Assert
    //-----------------------------------------------------------------------------------------------------------
    (await testAction.Should().ThrowAsync<XunitException>())
        .WithMessage("*ArgumentException*ABCDE*AggregateException*");
}

Expected behavior:

The test should pass, as we're asserting that an XunitException is thrown by ThrowExactlyAsync, indicating that the action throws an AggregateException instead of an ArgumentException.

Actual behavior:

The test fails, as ThrowExactlyAsync doesn't throw an exception as it treats the action as throwing an ArgumentException as it's an inner exception of the AggregateException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions