Skip to content

Assert.ThatAsync fail to catch expected exception #4907

@shlomi-borovitz-greenroad

Description

Given a simple test to check that an async method throws a certain exception -

namespace TestFailToFail;

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

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

    [Test]
    public void Test3()
    {
        Assert.That(() => throw new Exception("Ugh"), Throws.Exception);
    }
}

With NUnit 4.2.2 - all tests pass (as expected)
With NUnit 4.3.0 (and also 4.3.1) - both Test1 and Test2 fail with the thrown exception. Only the sync version - Test3 pass

 Test1
   Source: UnitTest1.cs line 6
   Duration: 8 ms

  Message: 
System.Exception : Ugh

  Stack Trace: 
<<Test1>b__0_0>d.MoveNext() line 8
--- End of stack trace from previous location ---
<<ThatAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
Constraint.ApplyToAsync[TActual](Func`1 taskDel)
Assert.ThatAsync(AsyncTestDelegate code, IResolveConstraint constraint, NUnitString message, String actualExpression, String constraintExpression)
Tests.Test1() line 8
GenericAdapter`1.GetResult()
AsyncToSyncAdapter.Await[TResult](TestExecutionContext context, Func`1 invoke)
AsyncToSyncAdapter.Await(TestExecutionContext context, Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass3_0.<PerformWork>b__0()
<>c__DisplayClass1_0`1.<DoIsolated>b__0(Object _)
ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
ContextUtils.DoIsolated(ContextCallback callback, Object state)
ContextUtils.DoIsolated[T](Func`1 func)
SimpleWorkItem.PerformWork()

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions