我正在尝试模拟对第三方服务的一些 API 调用以进行单元测试。我真的只是希望这个模拟函数返回相同的 RestEase.Response<...>
每次。
// Setup
var VeracrossMock = new Mock<IVeracrossAPI>(MockBehavior.Strict);
Func<List<VeracrossStudent>> func = () => new List<VeracrossStudent>() { new VeracrossStudent() { First_name = "Bob", Last_name = "Lob" } };
RestEase.Response<List<VeracrossStudent>> resp = new RestEase.Response<List<VeracrossStudent>>("", new HttpResponseMessage(HttpStatusCode.OK), func);
// Problem is on the line below
VeracrossMock.Setup(api => api.GetStudentsAsync(1, null, CancellationToken.None)).ReturnsAsync<RestEase.Response<List<VeracrossStudent>>>(resp);
它给了我一个红色下划线,然后声明 ReturnsAsync
不存在,或者至少按照我给出的论点不存在。
Error CS1929 'ISetup<IVeracrossAPI, Task<Response<List<VeracrossStudent>>>>' does not contain a definition for 'ReturnsAsync' and the best extension method overload 'SequenceExtensions.ReturnsAsync<Response<List<VeracrossStudent>>>(ISetupSequentialResult<Task<Response<List<VeracrossStudent>>>>, Response<List<VeracrossStudent>>)' requires a receiver of type 'ISetupSequentialResult<Task<Response<List<VeracrossStudent>>>>'
我应该如何使用 ReturnsAsync
?对如何 mock 这一点一无所知。
最佳答案
正在使用的通用参数与正在模拟的成员的参数不匹配。
删除通用参数
VeracrossMock
.Setup(_ => _.GetStudentsAsync(1, null, CancellationToken.None))
.ReturnsAsync(resp);
并且该方法将根据被模拟的成员推断所需的通用参数。
关于c# - Moq 不包含 ReturnAsync 的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50990487/