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
Labels
Comments
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 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. This special behavior for |
@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;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
When attempting to assert
ThrowsExactly<OperationCancelledException>
in an async method aTaskCancelledException
is thrown instead.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
The text was updated successfully, but these errors were encountered: