-
-
Notifications
You must be signed in to change notification settings - Fork 108
Description
Using TUnit 0.90.6, Visual Studio 2022 17.14.19, .NET 9.0, SDK 9.0.306
When validating that an exact ArgumentException is thrown and a subclass is thrown instead, fails to produce an error if the WithParameterName is used.
The following test correctly fails with the proper assertion:
[Test]
public async Task ThrowsExactly_works_as_expected()
{
await Assert.That(() => throw new ArgumentOutOfRangeException(paramName: "quantity", message: "must be less than 20"))
.ThrowsExactly<ArgumentException>();
}
Message:
Expected to throw exactly ArgumentException
but wrong exception type: ArgumentOutOfRangeException instead of exactly ArgumentExceptionat Assert.That(() => throw new ArgumentOutOfRangeException(paramName: "quantity", message: "must be less than 20")).ThrowsExactly()
However, if parameter name validation is added to the test, the test passes without error
[Test]
public async Task ThrowsExactly_WithParameterName_fails_to_throw()
{
await Assert.That(() => throw new ArgumentOutOfRangeException(paramName: "quantity", message: "must be less than 20"))
.ThrowsExactly<ArgumentException>()
.WithParameterName("quantity");
}
Flipping the exception types does produce an error
[Test]
public async Task ThrowsExactly_WithParameterName_does_throw()
{
await Assert.That(() => throw new ArgumentException(paramName: "quantity", message: "must be less than 20"))
.ThrowsExactly<ArgumentOutOfRangeException>()
.WithParameterName("quantity");
}
but the error does not say that the ArgumentOutOfRangeException was expected.
Message:
Expected ArgumentException to have parameter name "quantity"
but threw System.ArgumentExceptionat Assert.That(() => throw new ArgumentException(paramName: "quantity", message: "must be less than 20")).ThrowsExactly().WithParameterName("quantity")
Expectation is that all three of the above test cases fail.