Using Moq to mock an asynchronous method for a unit test

前端 未结 3 926
北海茫月
北海茫月 2020-11-30 18:44

I am testing a method for a service that makes a Web API call. Using a normal HttpClient works fine for unit tests if I also run the web service (l

3条回答
  •  囚心锁ツ
    2020-11-30 19:27

    With Mock.Of<...>(...) for async method you can use Task.FromResult(...):

    var client = Mock.Of(c => 
        c.PostAsync(It.IsAny(), It.IsAny()) == Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))
    );
    

提交回复
热议问题