Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible (with Moq) to 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 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?

like image 524
Luhmann Avatar asked Feb 09 '10 12:02

Luhmann


1 Answers

have you tried the following syntax:

repository.Setup(x => x.Where(It.IsAny<Func<T, ISpecification<T>>()).Returns(list);
like image 64
darthjit Avatar answered Nov 15 '22 00:11

darthjit