是否可以(使用Moq)使用Lambda参数进行存根方法调用?

如果我这样做:

var repository = new Mock<IRepository>(); repository.Setup(x => x.Where(banner => banner.Is.AvailableForFrontend())).Returns(list); 

“Where”是我的存储库中采用Func<T, ISpecification 。 AvailableForFrontend返回ISpecification的实现,list是存储库的generics类型的IEnumberable。

它编译得很好,但是当我运行我的测试时,我得到以下错误。

 ---- System.NotSupportedException : Expression banner => Convert((banner.Is.AvailableForFrontend() & banner.Is.SmallMediaBanner())) is not supported. 

如果我在存储库中使用Where的其他重载直接进行ISpecification,则没有问题。

所以我的新手模拟/ Moq问题是:我可以使用lamdba作为参数存根方法调用吗? 或者我应该以另一种方式解决这个问题?

你尝试过以下语法:

 repository.Setup(x => x.Where(It.IsAny>()).Returns(list);