diff --git a/TUnit.Assertions.Tests/Assertions/Delegates/Throws.WithParameterNameTests.cs b/TUnit.Assertions.Tests/Assertions/Delegates/Throws.WithParameterNameTests.cs index 0256927de9..41c2ca56c0 100644 --- a/TUnit.Assertions.Tests/Assertions/Delegates/Throws.WithParameterNameTests.cs +++ b/TUnit.Assertions.Tests/Assertions/Delegates/Throws.WithParameterNameTests.cs @@ -11,7 +11,7 @@ public async Task Fails_For_Different_Parameter_Name() var paramName1 = "foo"; var paramName2 = "bar"; var expectedMessage = """ - Expected ArgumentException to have parameter name "bar" + Expected ArgumentException to have parameter name "bar" (exact type) but ArgumentException parameter name was "foo" at Assert.That(action).ThrowsExactly().WithParameterName("bar") diff --git a/TUnit.Assertions/Conditions/ExceptionPropertyAssertions.cs b/TUnit.Assertions/Conditions/ExceptionPropertyAssertions.cs index 7eaa9b61a2..a07fb9ddd1 100644 --- a/TUnit.Assertions/Conditions/ExceptionPropertyAssertions.cs +++ b/TUnit.Assertions/Conditions/ExceptionPropertyAssertions.cs @@ -267,13 +267,16 @@ public class ExceptionParameterNameAssertion : Assertion where TException : Exception { private readonly string _expectedParameterName; + private readonly bool _requireExactType; public ExceptionParameterNameAssertion( AssertionContext context, - string expectedParameterName) + string expectedParameterName, + bool requireExactType = false) : base(context) { _expectedParameterName = expectedParameterName; + _requireExactType = requireExactType; } protected override Task CheckAsync(EvaluationMetadata metadata) @@ -291,6 +294,13 @@ protected override Task CheckAsync(EvaluationMetadata CheckAsync(EvaluationMetadata - $"ArgumentException to have parameter name \"{_expectedParameterName}\""; + _requireExactType + ? $"{typeof(TException).Name} to have parameter name \"{_expectedParameterName}\" (exact type)" + : $"ArgumentException to have parameter name \"{_expectedParameterName}\""; } diff --git a/TUnit.Assertions/Conditions/ThrowsAssertion.cs b/TUnit.Assertions/Conditions/ThrowsAssertion.cs index f18721e258..9f66e8d2c3 100644 --- a/TUnit.Assertions/Conditions/ThrowsAssertion.cs +++ b/TUnit.Assertions/Conditions/ThrowsAssertion.cs @@ -339,7 +339,7 @@ public ExceptionMessageMatchesAssertion WithMessageMatching(StringMa public ExceptionParameterNameAssertion WithParameterName(string expectedParameterName) { Context.ExpressionBuilder.Append($".WithParameterName(\"{expectedParameterName}\")"); - return new ExceptionParameterNameAssertion(Context, expectedParameterName); + return new ExceptionParameterNameAssertion(Context, expectedParameterName, requireExactType: true); } } diff --git a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet10_0.verified.txt b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet10_0.verified.txt index cdefb8c89e..f9b886b93f 100644 --- a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet10_0.verified.txt +++ b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet10_0.verified.txt @@ -669,7 +669,7 @@ namespace .Conditions public class ExceptionParameterNameAssertion : . where TException : { - public ExceptionParameterNameAssertion(. context, string expectedParameterName) { } + public ExceptionParameterNameAssertion(. context, string expectedParameterName, bool requireExactType = false) { } protected override .<.> CheckAsync(. metadata) { } protected override string GetExpectation() { } } diff --git a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet8_0.verified.txt b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet8_0.verified.txt index 738cb94062..d865b2f284 100644 --- a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet8_0.verified.txt +++ b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet8_0.verified.txt @@ -666,7 +666,7 @@ namespace .Conditions public class ExceptionParameterNameAssertion : . where TException : { - public ExceptionParameterNameAssertion(. context, string expectedParameterName) { } + public ExceptionParameterNameAssertion(. context, string expectedParameterName, bool requireExactType = false) { } protected override .<.> CheckAsync(. metadata) { } protected override string GetExpectation() { } } diff --git a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet9_0.verified.txt b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet9_0.verified.txt index b5a64ac00a..ff28d27625 100644 --- a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet9_0.verified.txt +++ b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet9_0.verified.txt @@ -669,7 +669,7 @@ namespace .Conditions public class ExceptionParameterNameAssertion : . where TException : { - public ExceptionParameterNameAssertion(. context, string expectedParameterName) { } + public ExceptionParameterNameAssertion(. context, string expectedParameterName, bool requireExactType = false) { } protected override .<.> CheckAsync(. metadata) { } protected override string GetExpectation() { } } diff --git a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.Net4_7.verified.txt b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.Net4_7.verified.txt index b0ea263886..e1aa8c8580 100644 --- a/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.Net4_7.verified.txt +++ b/TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.Net4_7.verified.txt @@ -644,7 +644,7 @@ namespace .Conditions public class ExceptionParameterNameAssertion : . where TException : { - public ExceptionParameterNameAssertion(. context, string expectedParameterName) { } + public ExceptionParameterNameAssertion(. context, string expectedParameterName, bool requireExactType = false) { } protected override .<.> CheckAsync(. metadata) { } protected override string GetExpectation() { } }