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

泪湿孤枕 提交于 2019-12-03 22:16:20

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

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...

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!