Skip to content

Commit

Permalink
SAVEPOINT
Browse files Browse the repository at this point in the history
  • Loading branch information
lg2de committed Jan 14, 2023
1 parent a4fa412 commit 3d60b71
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Src/FluentAssertions/AsyncAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ public static class AsyncAssertionsExtensions
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
public static async Task<AndWhichConstraint<GenericAsyncFunctionAssertions<T>, T>> WithResult<T>(
public static Task<AndWhichConstraint<GenericAsyncFunctionAssertions<T>, T>> WithResult<T>(
this Task<AndWhichConstraint<GenericAsyncFunctionAssertions<T>, T>> task,
T expected, string because = "", params object[] becauseArgs)
{
var andWhichConstraint = await task;
if (!task.IsCompleted) // TODO Diese Task ist nicht die richtige...
{
// The task is not completed, this means the previous assertion was failed.
// There is no result which can be checked.
return task;
}

var andWhichConstraint = task.Result;
var subject = andWhichConstraint.Subject;
subject.Should().Be(expected, because, becauseArgs);

return andWhichConstraint;
return task;
}

/// <summary>
Expand Down
25 changes: 25 additions & 0 deletions Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using FluentAssertions.Execution;
using FluentAssertions.Extensions;
using FluentAssertions.Specialized;
#if NETFRAMEWORK
using FluentAssertions.Specs.Common;
#endif
Expand Down Expand Up @@ -165,6 +167,29 @@ public async Task When_task_completes_late_it_in_assertion_scope_should_fail()
await action.Should().ThrowAsync<XunitException>();
}

[Fact]
public async Task When_task_does_not_completes_the_result_extension_does_not_hang()
{
// Arrange
var timer = new FakeClock();
var taskFactory = new TaskCompletionSource<int>();

// Act
Func<Task> action = () =>
{
Func<Task<int>> func = () => taskFactory.Task;
using var _ = new AssertionScope();
return func.Should(timer).CompleteWithinAsync(100.Milliseconds()).WithResult(2);
};

timer.Complete();

// Assert
var assertionTask = action.Should().ThrowAsync<XunitException>()
.WithMessage("Expected*to complete within 100ms.");
await this.Awaiting(x => assertionTask).Should().CompleteWithinAsync(100.Milliseconds());
}

[Fact]
public async Task When_task_consumes_time_in_sync_portion_it_should_fail()
{
Expand Down

0 comments on commit 3d60b71

Please sign in to comment.