n8wrl
n8wrl

Reputation: 19765

NUnit async tests fail after merge

NUnit 2.6.4.

I have a VS/C# project that introduces async methods. It has many tests like this that pass:

    [Test]
    public async void NullProcThrows_Async()
    {
        var keyList = new KeyList<int>();
        Assert.Throws<ArgumentNullException>(async () => await keyList.LoadAsync((IDBProcedure)null, "ID", CancellationToken.None));
    }

I have merged this into our trunk (no conflicts) and now this test fails. I am trying to figure out the difference.

When I trace the code in the trunk I see two exceptions thrown:

The first is the ArgumentNullException I am expecting. The second is

NUnit.Framework.AssertionException saying Expected
<System.ArgumentNullException> But was:  null

When I run the test on the branch version I only see the one exception and the test passes.

What could be different between the two projects?

Upvotes: 1

Views: 126

Answers (1)

David Pine
David Pine

Reputation: 24525

There seems to be a few issues with the code provided, consider the following:

[Test, ExpectedException(typeof(ArgumentNullException)]
public async Task NullProcThrows_Async()
{
    var keyList = new KeyList<int>();
    await keyList.LoadAsync((IDBProcedure)null, "ID", CancellationToken.None);
    Assert.Fail("This should never be executed as we expected the above to throw.");
}

According to the NUnit documentation, you should be using the ExpectedException attribute instead of Assert.Throws. So I added that attribute and then remove the Assert.Throws and instead add Assert.Fail. Also, I made the method Task returning and this prevented the async void. Finally, doing it this way prevents the async lambda.

Upvotes: 1

Related Questions