我刚刚升级到 NUnit 3,但在让我的单元测试通过时遇到了一些麻烦。
[Test]
public void GetSomethingAsync_CallsConverter()
{
int id = 123;
Assert.Catch<NullReferenceException>(
async () => await _repository.GetSomethingAsync(id));
_fooMock.Verify(f => f.Bar(id), Times.Once);
}
NUnit 给了我这个错误:
System.ArgumentException:不支持“异步无效”方法,请改用“异步任务”
我设法让我Assert.Throws
通过改变它这样的:
Assert.Throws<NullReferenceException>(
async () => await _repository.GetSomethingAsync(id));
//to
Assert.That(
async () => await _repository.GetSomethingAsync(id),
Throws.InstanceOf<NullReferenceException>());
但是没有类似的等价物Assert.Catch
。
那么我们应该如何使用Assert.Catch
异步方法呢?