用Moq模拟存储库时出现问题

| 我正在尝试用Moq模拟我的存储库。我正在尝试在我的存储库上模拟所有查询方法。我已经成功地模拟出了方法,以返回我所模拟出的所有类型的方法。 例:
mockProductRepo.Setup(x => x.GetAll()).Returns(products.AsQueryable());
但是,我在模拟使用另一种方法的方法时遇到了问题。例如,我的\“ FilterBy \”方法使用带表达式的Where子句返回对我的\“ GetAll \”方法的调用 示例:存储库方法
public virtual IQueryable<T> FilterBy(Expression<Func<T, bool>> expression)
{
   return GetAll().Where(expression);
}
更重要的是,我希望在助手类中模拟出存储库中的所有方法:
public static IRepository<Product> MockProductRepository(params Product[] products) {
        var mockProductRepo = new Mock<IRepository<Product>>();
        mockProductRepo.Setup(x => x.GetAll()).Returns(products.AsQueryable());
        mockProductRepo.Setup(x => x.FilterBy(It.IsAny<Expression<Func<Product, bool>>>())).Returns(products.AsQueryable().Where(It.IsAny<Expression<Func<Product, bool>>>()));
        return mockProductRepo.Object;
}
因此,除了上面模拟的FilterBy方法之外,还有一种方法可以将其设置为调用另一个模拟的方法,而不是上面的示例中的方法? 更新 我已经尝试了安装程序:
mockProductRepo.Setup(x => x.FilterBy(It.IsAny<Expression<Func<Product, bool>>>())).Returns(mockProductRepo.Object.GetAll().Where(It.IsAny<Expression<Func<Product, bool>>>()));
并且总是会错误地指出\“ Value不能为null。参数:predicate \”。从我对堆栈跟踪的了解来看,它在抱怨,因为我没有传递“ Where”谓词。我不确定在设置中如何表示传递到FilterBy方法中的表达式以在过滤器Where中使用。     
已邀请:
我自己想通了。
mockProductRepo.Setup(x => x.FilterBy(It.IsAny<Expression<Func<Product, bool>>>())).Returns((Expression<Func<Product,bool>> filter) => mockProductRepo.Object.GetAll().Where(filter));
    

要回复问题请先登录注册