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

Luh*_*ann 6 .net c# testing moq mocking

如果我这样做:

var repository = new Mock<IRepository<Banner>>();
repository.Setup(x => x.Where(banner => banner.Is.AvailableForFrontend())).Returns(list);
Run Code Online (Sandbox Code Playgroud)

"Where"是我的存储库中的一个方法Func<T, ISpecification<T>.AvailableForFrontend返回ISpecification的实现,list是存储库的泛型类型的IEnumberable.

它编译得很好,但是当我运行测试时出现以下错误.

---- System.NotSupportedException : Expression banner => Convert((banner.Is.AvailableForFrontend() & banner.Is.SmallMediaBanner())) is not supported.
Run Code Online (Sandbox Code Playgroud)

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

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

dar*_*jit 10

你尝试过以下语法:

repository.Setup(x => x.Where(It.IsAny<Func<T, ISpecification<T>>()).Returns(list);
Run Code Online (Sandbox Code Playgroud)