1

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?

n8wrl
  • 19,439
  • 4
  • 63
  • 103
  • What did you merge? – SLaks Jun 20 '16 at 14:56
  • It sounds like you're passing an `async void` lambda. – SLaks Jun 20 '16 at 14:56
  • Have you compared the code on your branch with the trunk to see what's different? It's highly likely that the merge "failed" (in the sense of merging the wrong code) and a diff will show up what it replaced incorrectly. – ChrisF Jun 20 '16 at 14:58
  • `now this test fails` - on your CI server running a different version of NUnit? The NUnit semantics around `async void` methods have changed more than once. – Stephen Cleary Jun 20 '16 at 15:03
  • This branch added some async versions of sync methods on repositories. The to/from code is identical, as far as I can tell. In this case, LoadAsync has this method: public async Task LoadAsync(IDBProcedure p, string keyColumnName, CancellationToken ct) – n8wrl Jun 20 '16 at 15:03
  • Well now that's spooky. I forced VS to get-latest from TFS overwriting files even if it thought they were the same and now the tests pass. – n8wrl Jun 20 '16 at 15:24

1 Answers1

1

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.

David Pine
  • 23,787
  • 10
  • 79
  • 107
  • Thank you David. I will consider this as an update. I was in a yank to get the merge successful after our release over the weekend. Everything tested fine in the branch and didn't in the trunk. so some bits must have been fiddled because a complete get-latest solved the problem. – n8wrl Jun 20 '16 at 20:46