NSubstitute - Received for async - “call is not awaited”warning

JBond picture JBond · Jul 9, 2015 · Viewed 13.9k times · Source

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?

Answer

Marcio Rinaldi picture Marcio Rinaldi · Jul 20, 2015

As soon as you update to version 1.9.0 or higher, you'll be able to use the await without receiving a NullReferenceException.