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?

4

1 回答 1

1

提供的代码似乎存在一些问题,请考虑以下几点:

[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.");
}

根据NUnit文档,您应该使用ExpectedException属性而不是Assert.Throws. 所以我添加了该属性,然后删除了Assert.Throws,而是添加了Assert.Fail. 另外,我让方法Task返回,这阻止了async void. 最后,这样做可以防止async lambda.

于 2016-06-20T19:55:52.967 回答