Skip to content

ThrowWithinAsync does not work with TaskCanceledException #2444

@Arithmomaniac

Description

@Arithmomaniac

Description

ThrowWithinAsync does not work with TaskCanceledException, but only AFAICT on async/await methods.

See the reproduction steps, which endeavor to isolate the cause.

Reproduction Steps

await Invoking(async () => { await Task.Delay(1); throw new TaskCanceledException(); })
    .Should().ThrowAsync<TaskCanceledException>();
await Invoking(async () => { await Task.Delay(1); throw new InvalidOperationException(); })
    .Should().ThrowWithinAsync<InvalidOperationException>(TimeSpan.FromSeconds(1));
await Invoking(() => Task.FromException(new TaskCanceledException()) )
    .Should().ThrowWithinAsync<TaskCanceledException>(TimeSpan.FromSeconds(0.5));
await Invoking(async () => { await Task.Delay(1); throw new TaskCanceledException(); })
    .Should().ThrowWithinAsync<TaskCanceledException>(TimeSpan.FromSeconds(1));

Expected behavior

Test passes; all four assertions work

Actual behavior

Test fails with the following exception, which indicates only the last assertion failed:

  Message: 
    Expected a <System.Threading.Tasks.TaskCanceledException> to be thrown within 1s, but no exception was thrown.

  Stack Trace: 
    XUnit2TestFramework.Throw(String message)
    TestFrameworkProvider.Throw(String message)
    DefaultAssertionStrategy.HandleFailure(String message)
    AssertionScope.FailWith(Func`1 failReasonFunc)
    AssertionScope.FailWith(Func`1 failReasonFunc)
    AssertionScope.FailWith(String message)
    AsyncFunctionAssertions`2.AssertThrows[TException](Exception exception, TimeSpan timeSpan, String because, Object[] becauseArgs)
    AsyncFunctionAssertions`2.ThrowWithinAsync[TException](TimeSpan timeSpan, String because, Object[] becauseArgs)
    CollectionExtensionsTests.Foo() line 29
    --- End of stack trace from previous location --

Regression?

No response

Known Workarounds

No response

Configuration

xUnit.net VSTest Adapter v2.5.0.1+5ebf84cd75 (64-bit .NET 7.0.13)
FluentAssertions 6.12.0

Other information

No response

Are you willing to help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions