c#-用MOQ模拟异步获取方法
作者:互联网
我如何摆脱此错误消息:
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>()));
标签:async-await,moq,c,net 来源: https://codeday.me/bug/20191120/2041704.html