diff --git a/TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs b/TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs new file mode 100644 index 0000000000..ee74598983 --- /dev/null +++ b/TUnit.Assertions.Tests/ThrowInDelegateValueAssertionTests.cs @@ -0,0 +1,28 @@ +using TUnit.Assertions.AssertConditions.Throws; + +namespace TUnit.Assertions.Tests; + +public class ThrowInDelegateValueAssertionTests +{ + [Test] + public async Task ThrowInDelegateValueAssertion_ReturnsExpectedErrorMessage() + { + var assertion = async () => await Assert.That(() => + { + throw new Exception("No"); + return true; + }).IsEqualTo(true); + + await Assert.That(assertion) + .Throws() + .WithMessageContaining(""" + Expected () => + { + throw new Exception("No"); + return true; + } to be equal to True + + but An exception was thrown during the assertion: System.Exception: No + """); + } +} diff --git a/TUnit.Assertions/AssertConditions/ExpectedValueAssertCondition.cs b/TUnit.Assertions/AssertConditions/ExpectedValueAssertCondition.cs index d8e4219395..343c0dd9f3 100644 --- a/TUnit.Assertions/AssertConditions/ExpectedValueAssertCondition.cs +++ b/TUnit.Assertions/AssertConditions/ExpectedValueAssertCondition.cs @@ -14,7 +14,7 @@ public void WithTransform(Func actualTransformation, { _transformations.Add((actualTransformation, expectedTransformation)); } - + public void WithComparer(Func comparer) { _customComparers.Add(comparer); @@ -26,13 +26,13 @@ AssertionMetadata assertionMetadata ) { var expected = ExpectedValue; - + foreach (var (actualTransformation, expectedTransformation) in _transformations) { actualValue = actualTransformation(actualValue); expected = expectedTransformation(expected); } - + foreach (var result in _customComparers.Select(customComparer => customComparer(actualValue, expected))) { switch (result) @@ -44,8 +44,13 @@ AssertionMetadata assertionMetadata } } + if (exception is not null) + { + return FailWithMessage($"An exception was thrown during the assertion: {exception}"); + } + return GetResult(actualValue, expected); } - + protected abstract ValueTask GetResult(TActual? actualValue, TExpected? expectedValue); -} \ No newline at end of file +}