Skip to content

Commit

Permalink
Add ApplyToAsync override to ThrowsExceptionConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Dec 24, 2024
1 parent 1f1959a commit cca65aa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Threading.Tasks;
using NUnit.Framework.Internal;

namespace NUnit.Framework.Constraints
Expand Down Expand Up @@ -42,6 +43,14 @@ public override ConstraintResult ApplyTo<TActual>(ActualValueDelegate<TActual> d
return ApplyTo((Delegate)del);
}

/// <inheritdoc/>
public override async Task<ConstraintResult> ApplyToAsync<TActual>(Func<Task<TActual>> actual)
{
var exception = await ExceptionHelper.RecordExceptionAsync(actual, nameof(actual));

return new ThrowsExceptionConstraintResult(this, exception);
}

#region Nested Result Class

private class ThrowsExceptionConstraintResult : ConstraintResult
Expand Down
12 changes: 12 additions & 0 deletions src/NUnitFramework/tests/Assertions/AssertThatAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ await Assert.ThatAsync(() => Task.FromCanceled(cancel.Token),
.With.Property(nameof(TaskCanceledException.CancellationToken)).EqualTo(cancel.Token));
}

[Test]
public async Task AssertionPasses_FaultedDelegate_ThrowsAnyException()
{
await Assert.ThatAsync(() => throw new Exception("Ugh"), Throws.Exception);
}

[Test]
public async Task AssertionPasses_FaultedTask_ThrowsAnyException()
{
await Assert.ThatAsync(() => Task.FromException(new Exception("Ugh")), Throws.Exception);
}

[Test]
public async Task AssertionPasses_FaultedTask_ThrowsMatchingException()
{
Expand Down

0 comments on commit cca65aa

Please sign in to comment.