How can I tell Moq to return a Task?

前端 未结 4 1712
盖世英雄少女心
盖世英雄少女心 2020-11-27 09:56

I\'ve got an interface which declares

Task DoSomethingAsync();

I\'m using MoqFramework for my tests:

[TestMethod()]
public          


        
4条回答
  •  不知归路
    2020-11-27 10:19

    Similar Issue

    I have an interface that looked roughly like:

    Task DoSomething(int arg);
    

    Symptoms

    My unit test failed when my service under test awaited the call to DoSomething.

    Fix

    Unlike the accepted answer, you are unable to call .ReturnsAsync() on your Setup() of this method in this scenario, because the method returns the non-generic Task, rather than Task.

    However, you are still able to use .Returns(Task.FromResult(default(object))) on the setup, allowing the test to pass.

提交回复
热议问题