如何摆脱此错误消息:
Error 5 Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<TGB.Business.DTO.SchoolyearDTO>>' to 'System.Collections.Generic.IEnumerable<TGB.Business.DTO.SchoolyearDTO>'. An explicit conversion exists (are you missing a cast?)
我以为我的 Task.FromResult 会解决这个问题,但没有......
mockService.Setup<IEnumerable<SchoolyearDTO>>(c => c.GetSchoolyears()).Returns(
Task.FromResult(Enumerable.Empty<SchoolyearDTO>()));
public async Task<IEnumerable<SchoolyearDTO>> GetSchoolyearsAsync()
{
var schoolyears = await ...
}
最佳答案
GetSchoolyearsAsync
是一个异步方法,所以它返回一个 Task<IEnumerable<SchoolyearDTO>>
而不仅仅是 IEnumerable<SchoolyearDTO>
.您需要在 SetupGet
的类型参数中指定
mockService.SetupGet<Task<IEnumerable<SchoolyearDTO>>>(c => c.GetSchoolyears()).
Returns(Task.FromResult(Enumerable.Empty<SchoolyearDTO>()));
关于c# - 使用 MOQ 模拟异步 Get 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909855/