问题
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?
回答1:
As soon as you update to version 1.9.0 or higher, you'll be able to use the await
without receiving a NullReferenceException
.
回答2:
Whenever the Received()
predicate gets too complicated or just won't match with NSubstitute you can always capture the specified args using callbacks via When().Do()
or .AndDoes()
. For your use case that would go something like this
DatabaseParams receivedParms = null;
databaseHelperSub.ExecuteProcAsync(Arg.Any<DatabaseParams>())
.Returns(Task.FromResult((object)null))
.AndDoes(x => receivedParms = x.Arg<DatabaseParams>);
//Act
await underTest.ExecuteAsync(request);
//Assert
receivedParms.Should().NotBeNull();
// assert your parms...
回答3:
The Jake Ginnivan answer explains that for Received await is not required, however compiler doesn’t understand it.
You can safely suppress the warning
#pragma warning disable 4014 //for .Received await is not required, so suppress warning “Consider applying the 'await' operator”
_service.Received(totalNumber).MyMethod(Arg.Any<ParamType>());
#pragma warning restore 4014
来源:https://stackoverflow.com/questions/31318594/nsubstitute-received-for-async-call-is-not-awaited-warning