Is it possible (with Moq) to stub method calls with Lambda parameters?

笑着哭i 提交于 2019-12-03 10:01:48

问题


If I do this:

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

"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.

If i use my other overload of Where on the repository that takes a ISpecification directly, theres no problem.

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?


回答1:


have you tried the following syntax:

repository.Setup(x => x.Where(It.IsAny<Func<T, ISpecification<T>>()).Returns(list);


来源:https://stackoverflow.com/questions/2228845/is-it-possible-with-moq-to-stub-method-calls-with-lambda-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!