我在存储库方法中使用 GetMany() 的 lambda 表达式来过滤大量值中的一些值。它在 Mvc 代码中运行良好。但是当我编写单元测试时,我为存储库做了一个模拟,并给出了如下所示的设置
focusRepository.Setup(x => x.GetMany(It.IsAny<Expression<Func<Focus, bool>>>())).Returns(fakeFocus);
但这不起作用。它没有过滤值。相反,它返回 null 或所有值。整个测试方法是
IEnumerable<Focus> fakeFocus = new List<Focus>
{
new Focus { FocusId = 1, FocusName="Test1",GroupId = 1},
new Focus { FocusId = 2, FocusName="Test2",GroupId = 1},
new Focus { FocusId = 3, FocusName="Test3",GroupId = 2}
}.AsEnumerable();
focusRepository.Setup(x => x.GetMany(It.IsAny<Expression<Func<Focus, bool>>>())).Returns(fakeFocus);
GroupGoalFormModel goal = new GroupGoalFormModel();
GroupController controller = new GroupController();
ViewResult result = controller.CreateGoal(2) as ViewResult;
Assert.IsNotNull(result, "View Result is null");
Assert.IsInstanceOf(typeof(GroupGoalFormModel),
result.ViewData.Model, "Wrong View Model");
在我的存储库中,我将代码编写为
IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
谁能帮帮我吗