使用 MOQ 模拟异步 Get 方法
Mock async Get method with 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
是一个异步方法,所以它 returns 一个 Task<IEnumerable<SchoolyearDTO>>
而不仅仅是一个 IEnumerable<SchoolyearDTO>
。您需要在 SetupGet
的类型参数中指定
mockService.SetupGet<Task<IEnumerable<SchoolyearDTO>>>(c => c.GetSchoolyears()).
Returns(Task.FromResult(Enumerable.Empty<SchoolyearDTO>()));
如何摆脱此错误消息:
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
是一个异步方法,所以它 returns 一个 Task<IEnumerable<SchoolyearDTO>>
而不仅仅是一个 IEnumerable<SchoolyearDTO>
。您需要在 SetupGet
mockService.SetupGet<Task<IEnumerable<SchoolyearDTO>>>(c => c.GetSchoolyears()).
Returns(Task.FromResult(Enumerable.Empty<SchoolyearDTO>()));