Stubbing Task returning method in async unit test

前端 未结 2 1013
闹比i
闹比i 2021-01-01 12:00

Let\'s say I have the following class and an interface it depends on:

public class MyController
{
    private IRepository _repository;
    public MyControlle         


        
相关标签:
2条回答
  • 2021-01-01 12:42

    You will need to return something; async Task methods cannot ever return null.

    The T doesn't matter. You could declare a static readonly Task SuccessTask = Task.FromResult<object>(null); as a helper constant if you want. I have similar constants in my AsyncEx library.

    0 讨论(0)
  • 2021-01-01 12:51

    If you are targeting .NET Framework 4.6 or later, you can now use .Returns(Task.CompletedTask) instead which is slightly shorter.

    0 讨论(0)
提交回复
热议问题