-
Notifications
You must be signed in to change notification settings - Fork 731
Description
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.