NSubstitute-异步接收-“未等待呼叫"警告 [英] NSubstitute - Received for async - “call is not awaited”warning
问题描述
我正在尝试验证是否使用正确的参数调用了异步方法.但是,我得到警告:
由于未等待此调用,因此在调用完成之前将继续执行当前方法.请考虑将'await'运算符应用于调用结果".该警告出现在//Assert
注释下方的代码行(如下).
我使用 NSubstitute 进行的测试如下:
[Test]
public async Task SimpleTests()
{
//Arrange
var request = CreateUpdateItemRequest();
databaseHelperSub.ExecuteProcAsync(Arg.Any<DatabaseParams>()).Returns(Task.FromResult((object)null));
//Act
await underTest.ExecuteAsync(request);
//Assert
databaseHelperSub.Received().ExecuteProcAsync(Arg.Is<DatabaseParams>(
p => p.StoredProcName == StoredProcedureName
&& p.Parameters[0].ParameterName == "Param1"
&& p.Parameters[0].Value.ToString() == "Value1"
&& p.Parameters[1].ParameterName == "Param2"
&& p.Parameters[1].Value.ToString() == "Value2"));
}
被测单元underTest.ExecuteAsync(request)
调用ExecuteProcedureAsync
并执行等待:
var ds = await DatabaseHelper.ExecuteProcAsync(dbParams);
由于使用NSubstitute的事实,在执行被测单元之后需要Received().而在RhinoMocks中,您可以期望在执行被测单元之前进行呼叫. RhinoMocks可以返回Task.FromResult(),而NSubstitute不能.
等效的RhinoMocks是这样的:
[Test]
public async Task SimpleTest()
{
// Arrange
var request = new UpdateItemRequest();
databaseHelperMock.Expect(m => m.ExecuteProcAsync(Arg<DatabaseParams>.Matches(
p => p.StoredProcName == StoredProcedureName
&& p.Parameters[0].ParameterName == "Param1"
&& p.Parameters[0].Value.ToString() == "Value1"
&& p.Parameters[1].ParameterName == "Param2"
&& p.Parameters[1].Value.ToString() == "Value2
))).Return(Task.FromResult<object>(null));
// Act
await underTest.ExecuteAsync(request);
}
我看到有一种解决方法,您可以添加扩展方法来消除此问题:
public static class TestHelper
{
public static void IgnoreAwait(this Task task)
{
}
}
我的 NSubstitute 测试行可以按以下步骤执行,并且警告消失:
databaseHelperSub.Received().ExecuteProcAsync(Arg.Is<DatabaseParams>(
p => p.StoredProcName == StoredProcedureName
&& p.Parameters[0].ParameterName == "Param1"
&& p.Parameters[0].Value.ToString() == "Value1"
&& p.Parameters[1].ParameterName == "Param2"
&& p.Parameters[1].Value.ToString() == "Value2")).IgnoreAwait();
}
但是,我认为对此必须有更好的解决方案?
一旦更新到1.9.0或更高版本,您将能够使用await
而不会收到NullReferenceException
.>
I am trying to verify that an asynchronous method was called with the correct parameters. However, I get the warning:
"Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call". This warning appears on the line of code underneath the //Assert
comment (below).
My test using NSubstitute is as follows:
[Test]
public async Task SimpleTests()
{
//Arrange
var request = CreateUpdateItemRequest();
databaseHelperSub.ExecuteProcAsync(Arg.Any<DatabaseParams>()).Returns(Task.FromResult((object)null));
//Act
await underTest.ExecuteAsync(request);
//Assert
databaseHelperSub.Received().ExecuteProcAsync(Arg.Is<DatabaseParams>(
p => p.StoredProcName == StoredProcedureName
&& p.Parameters[0].ParameterName == "Param1"
&& p.Parameters[0].Value.ToString() == "Value1"
&& p.Parameters[1].ParameterName == "Param2"
&& p.Parameters[1].Value.ToString() == "Value2"));
}
The unit under test method underTest.ExecuteAsync(request)
calls ExecuteProcedureAsync
and performs the await:
var ds = await DatabaseHelper.ExecuteProcAsync(dbParams);
Due to the fact that with NSubstitute, the Received() is required after the execution of the unit under test. Whereas in RhinoMocks, you can expect for a call to occur before the unit under test is executed. RhinoMocks can return the Task.FromResult() whereas NSubstitute cannot.
The RhinoMocks equivalent that works is this:
[Test]
public async Task SimpleTest()
{
// Arrange
var request = new UpdateItemRequest();
databaseHelperMock.Expect(m => m.ExecuteProcAsync(Arg<DatabaseParams>.Matches(
p => p.StoredProcName == StoredProcedureName
&& p.Parameters[0].ParameterName == "Param1"
&& p.Parameters[0].Value.ToString() == "Value1"
&& p.Parameters[1].ParameterName == "Param2"
&& p.Parameters[1].Value.ToString() == "Value2
))).Return(Task.FromResult<object>(null));
// Act
await underTest.ExecuteAsync(request);
}
I have seen that there is a workaround where you can add an extension method to remove the issue:
public static class TestHelper
{
public static void IgnoreAwait(this Task task)
{
}
}
Meaning my test line for NSubstitute can be executed as follows and the warning goes away:
databaseHelperSub.Received().ExecuteProcAsync(Arg.Is<DatabaseParams>(
p => p.StoredProcName == StoredProcedureName
&& p.Parameters[0].ParameterName == "Param1"
&& p.Parameters[0].Value.ToString() == "Value1"
&& p.Parameters[1].ParameterName == "Param2"
&& p.Parameters[1].Value.ToString() == "Value2")).IgnoreAwait();
}
However, I assumed there must be a better solution out there for this?
As soon as you update to version 1.9.0 or higher, you'll be able to use the await
without receiving a NullReferenceException
.
这篇关于NSubstitute-异步接收-“未等待呼叫"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!