是否有可能(与MOQ)存根和λ参数的方法调用? [英] Is it possible (with Moq) to stub method calls with Lambda parameters?
问题描述
如果我这样做:
var repository = new Mock<IRepository<Banner>>();
repository.Setup(x => x.Where(banner => banner.Is.AvailableForFrontend())).Returns(list);
去哪儿是我仓库里的方法,需要一个 Func键&LT; T,ISpecification&LT; T&GT;
。 AvailableForFrontend返回ISpecification的实施,单是仓库的泛型类型的IEnumberable。
"Where" is a method on my repository that takes a Func<T, ISpecification<T>
. AvailableForFrontend returns an implementation of ISpecification, and list is an IEnumberable of the generic type of the repository.
它编译罚款,但我得到以下错误,当我运行我的测试。
It compiles fine, but i get the following error when I run my tests.
---- System.NotSupportedException : Expression banner => Convert((banner.Is.AvailableForFrontend() & banner.Is.SmallMediaBanner())) is not supported.
如果我用我的其他的凡直接接受一个ISpecification信息库,世界上没有问题,过载。
If i use my other overload of Where on the repository that takes a ISpecification directly, theres no problem.
所以,我的新手模拟/最小起订量的问题是:我可以存根方法调用了lamdba作为参数?或者我应该去了解这个以另一种方式?
So my newbie mock / Moq question is: Can I stub a method call with a lamdba as parameter? Or should I go about this in another way?
推荐答案
你有没有尝试过的语法如下:
have you tried the following syntax:
repository.Setup(x => x.Where(It.IsAny<Func<T, ISpecification<T>>()).Returns(list);
这篇关于是否有可能(与MOQ)存根和λ参数的方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!