Skip to content

Commit 1b9c010

Browse files
authored
Fix Assert.Throws Type Check (#2540)
* Fix Assert.Throws Type Check * Update Throws.ExceptionTests.cs * Update Throws.ExceptionTests.cs
1 parent b289ea6 commit 1b9c010

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

TUnit.Assertions.Tests/Assertions/Delegates/Throws.ExceptionTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ await Assert.That(sut).ThrowsException()
2727
}
2828

2929
[Test]
30-
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
30+
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application assemblies.", Justification = "Test method.")]
3131
public async Task Returns_Exception_When_Awaited()
3232
{
3333
Exception exception = CreateCustomException();
@@ -49,5 +49,23 @@ public async Task Succeeds_For_Code_With_Exceptions()
4949

5050
await Assert.That(sut).ThrowsNothing();
5151
}
52+
53+
private void ThrowsArgumentException()
54+
=> throw new ArgumentException("Just for testing");
55+
56+
[Test]
57+
public async Task ThrowsAsync_DoesNotCheckType()
58+
{
59+
await Assert.That(async () =>
60+
await Assert.ThrowsAsync<OverflowException>(LocalTestFunction)
61+
).Throws<AssertionException>();
62+
return;
63+
64+
Task LocalTestFunction()
65+
{
66+
ThrowsArgumentException();
67+
return Task.CompletedTask;
68+
}
69+
}
5270
}
5371
}

TUnit.Assertions/Assertions/Throws/ThrowsExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class ThrowsExtensions
2121
public static ThrowsException<object?, Exception> Throws(this IDelegateSource delegateSource, Type type, [CallerArgumentExpression("type")] string? doNotPopulateThisValue = null)
2222
{
2323
return new ThrowsException<object?, Exception>(
24-
delegateSource.RegisterAssertion(new ThrowsOfTypeAssertCondition<object?, Exception>(), [doNotPopulateThisValue]),
24+
delegateSource.RegisterAssertion(new ThrowsOfTypeAssertCondition(type), [doNotPopulateThisValue]),
2525
delegateSource,
2626
e => e);
2727
}
@@ -82,4 +82,4 @@ public static ThrowsException<TActual, TException> WithParameterName<TActual, TE
8282
throwsException.RegisterAssertion((selector) => new ThrowsWithParamNameAssertCondition<TActual, TException>(expected, StringComparison.Ordinal, ex => selector(ex) as ArgumentException), [doNotPopulateThisValue]);
8383
return throwsException;
8484
}
85-
}
85+
}

0 commit comments

Comments
 (0)