Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to assert ThrowsExactly<OperationCancelledException> in async method #875

Closed
alterius opened this issue Jul 18, 2018 · 2 comments
Closed

Comments

@alterius
Copy link

Description

When attempting to assert ThrowsExactly<OperationCancelledException> in an async method a TaskCancelledException is thrown instead.

[Test]
public void FluentAssertions_ThrowsCorrectException()
{
    var ex = new OperationCanceledException();

    Func<Task> action = async () => await ThrowException(ex);

    action.Should().ThrowExactly<OperationCanceledException>()
        .Which.Should().Be(ex);
}

[Test]
public void Nunit_ThrowsCorrectException()
{
    var ex = new OperationCanceledException();

    var result = Assert.Throws<OperationCanceledException>(async () => await ThrowException(ex));

    Assert.AreSame(ex, result);
}

public Task ThrowException(Exception ex)
{
    throw ex;
}

Expected behavior:

Both example tests should throw an OperationCancelledException and report passed.

Actual behavior:

The FluentAssertions test fails because a different TaskCancelledException is thrown.

Versions

FluentAssertions 5.4.1
NUnit 2.6.4

@jnyrup
Copy link
Member

jnyrup commented Aug 13, 2018

Some observations using NUnit 3.10.1 instead:

var result = Assert.Throws<OperationCanceledException>(async () => await ThrowException(ex));

throws

System.ArgumentException: ''async void' methods are not supported, please use 'async Task' instead'

and the dedicated ThrowsAsync

var result = Assert.ThrowsAsync<OperationCanceledException>(async () => await ThrowException(ex));

throws

NUnit.Framework.AssertionException: '  Expected: <System.OperationCanceledException>
  But was:  <System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at NUnit.Framework.Internal.AsyncInvocationRegion.AsyncTaskInvocationRegion.WaitForPendingOperationsToComplete(Object invocationResult) in C:\src\nunit\nunit\src\NUnitFramework\framework\Internal\AsyncInvocationRegion.cs:line 113
   at NUnit.Framework.Assert.ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, String message, Object[] args) in C:\src\nunit\nunit\src\NUnitFramework\framework\Assert.Exceptions.Async.cs:line 51>
'

When using another kind of exception, e.g. ArgumentNullException it works fine.

This special behavior for OperationCanceledException seems to stem from the source:

@jnyrup
Copy link
Member

jnyrup commented Sep 21, 2019

@alterius This issue seems to have been fixed.

Using Fluent Assertions 5.9.0 and NUnit 3.12 all these tests passes

[Test]
public void FluentAssertions_ThrowsCorrectException()
{
    var ex = new OperationCanceledException();

    Func<Task> action = () => ThrowException(ex);

    action.Should().ThrowExactly<OperationCanceledException>()
        .Which.Should().Be(ex);
}

[Test]
public async Task FluentAssertions_ThrowsCorrectException_async()
{
    var ex = new OperationCanceledException();

    Func<Task> action = () => ThrowException(ex);

    (await action.Should().ThrowExactlyAsync<OperationCanceledException>())
        .Which.Should().Be(ex);
}

[Test]
public void FluentAssertions_ThrowsCorrectException_await()
{
    var ex = new OperationCanceledException();

    Func<Task> action = async () => await ThrowException(ex);

    action.Should().ThrowExactly<OperationCanceledException>()
        .Which.Should().Be(ex);
}

[Test]
public async Task FluentAssertions_ThrowsCorrectException_async_await()
{
    var ex = new OperationCanceledException();

    Func<Task> action = async () => await ThrowException(ex);

    (await action.Should().ThrowExactlyAsync<OperationCanceledException>())
        .Which.Should().Be(ex);
}

private Task ThrowException(Exception ex)
{
    throw ex;
}

@jnyrup jnyrup closed this as completed Sep 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants