views:

296

answers:

1

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?

+3  A: 

have you tried the following syntax:

repository.Setup(x => x.Where(It.IsAny<Func<T, ISpecification<T>>()).Returns(list);
darthjit
+1 That was exactly what i needed. Thanks.
Luhmann
What about verifying that a method has been called with a specific lambda expression as a parameter? Is it possible with Moq?repository.Verify(x => x.Where(banner => banner.Is.AvailableForFrontend()));
Enrico Campidoglio
yes you can if you specify that it is verifiable while setting up.repository.Setup(x => x.Where(It.IsAny<Func<T, ISpecification<T>>()).Returns(list).Verifiable();here is the link to a setup verify example using moq:http://dotnet.dzone.com/news/beginning-mocking-moq-3-part-3
darthjit