Is it possible (using Moq) to call the stub method calls with lambda parameters?

If I do this:

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

Where is the method in my repository that accepts Func<T, ISpecification<T> . AvailableForFrontend returns an implementation of ISpecification, and the list is an IEnumberable of a generic repository type.

It compiles fine, but I get the following error while running my tests.

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

If I use my other Where in the repository overload, which accepts ISpecification directly, the problem does not occur.

So my question is about my newbie's mockups / monk: Is it possible to drown out a method call using the lamdba parameter as a parameter? Or should I go about it differently?

+6
c # testing moq mocking
source share
1 answer

You tried the following syntax:

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

All Articles