The methods I need to stub are called e.g. like this:
List<DocumentInfo> documentInfosToDelete = await _documentInfoRepository.GetListByExternalIdAsync(
partyLegalEntity,
externalId,
type,
status);
This works, but generates a compiler warning: 'This async method lacks 'await' operators' etc.
testService.DocumentInfoRepos.GetListByExternalIdAsyncStringStringDocumentTypeDocumentStatus =
(async (a, b, c, d) =>
{
GetListByExternalIdAsyncCalled = true;
return new List<DocumentInfo>();
});
I want to get rid of those warnings, as they would drown out any warnings about actual problems.
Returning new Task(...) hangs. If I understand what I have found on the WWW correctly, Task.Run(...) does something completely different from the await async pattern we are using.
What to do?
Returning new Task(…)
hangs, because that creates an unstarted Task
. What you can do instead is to create an already completed Task
by calling Task.FromResult
:
testService.DocumentInfoRepos.GetListByExternalIdAsyncStringStringDocumentTypeDocumentStatus =
(a, b, c, d) =>
{
GetListByExternalIdAsyncCalled = true;
return Task.FromResult(new List<DocumentInfo>());
};
This code will behave the same as an async
method with no await
, but it will not produce a warning.