使用Where()的Moq Return [英] Moq Return using Where()
本文介绍了使用Where()的Moq Return的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试建立一个模拟以从集合中返回数据:
I'm trying to set up a mock to return data from a collection:
private IList<DutyCategory> dutyCategories;
private Mock<IDutyCategoryService> mockService;
[TestInitialize()]
public void UnitTestSetup()
{
dutyCategories = new List<DutyCategory>()
{
new DutyCategory(){Description = "Description",ID = 1,IsActive = true,Name = "Test 1",OrganizationID = 1}
};
mockService = new Mock<IDutyCategoryService>();
mockService.Setup(a => a.GetDutyCategories()).ReturnsAsync(dutyCategories);
mockService.Setup(a => a.GetDutyCategoriesByOrganization(It.IsAny<int>()))
.ReturnsAsync((int id) => dutyCategories.Where(n=>n.OrganizationID == id));
}
UnitTestSetup中的最后一行是问题所在.我收到以下错误:
That last line in the UnitTestSetup is the problem. I get the following error:
无法将lambda表达式转换为类型'System.Collections.Generic.IEnumerable',因为它不是委托类型
Cannot convert lambda expression to type 'System.Collections.Generic.IEnumerable' because it is not a delegate type
我该如何模拟GetDutyCategoriesByOrganization,以便它返回列表勤奋类别的子集?
How do i mock GetDutyCategoriesByOrganization so that it returns a subset of list dutyCategories?
推荐答案
这似乎有效:
.Returns<int>(i => Task.FromResult(dutyCategories.Where(n => n.OrganizationID == i)));
这篇关于使用Where()的Moq Return的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文