Setup with method that takes lambda expression raises NotSupportedException

1,930 views
Skip to first unread message

David Hadiprijanto

unread,
Oct 28, 2009, 11:09:47 AM10/28/09
to Moq Discussions

Newbie question.

I have the following code using Moq:

var mockRepository = new Mock<IRepository>();
var someTypeInstance = new SomeType();
someTypeInstance.Id = Guid.NewGuid();
someTypeInstance.Name = "Mock Name";
mockRepository.Setup(r => r.Load<SomeType>(st => st.Id ==
Guid.Empty)).Returns(someTypeInstance);

during runtime, on that last line above, it raises the following
exception:
System.NotSupportedException: Expression st => st.Id = Guid.Empty is
not supported..

If I change the last line to the following, it raises the following
exception:
var fakeGuid = Guid.NewGuid();
mockRepository.Setup(r => r.Load<SomeType>(st => st.Id ==
fakeGuid )).Returns(someTypeInstance);

System.NotSupportedException: Expression st => (st.Id = value
(TestClassFullName+<>c__DisplayClass33).fakeGuid) is not supported..

However, if I change the last line to the following, it seems fine:

mockRepository.Setup(r => r.Load<SomeType>(st => true)).Returns
(someTypeInstance);

So, is it true that Moq does not support mocking method call that
takes lambda expression? If not, how would I mock such method?

The interface definition for corr. IRepository.Load is :

T Load<T>(Expression<Func<T, bool>> whereCondition);

Thanks...


David Hadiprijanto

unread,
Oct 29, 2009, 4:25:12 PM10/29/09
to Moq Discussions

For what is worth, I think this is a limitation in Moq.

I found this link (http://groups.google.com/group/moqdisc/
browse_thread/thread/20f5a2ceefcba0e/8f477c1f5c4b5321?
lnk=gst&q=func#8f477c1f5c4b5321)

that is references from this StackOverflow discussion (http://
stackoverflow.com/questions/288413/moq-expect-on-irepository-passing-
expression).

The comment from Daniel Cazzulino is about a year old, so I don't know
if the issue/limitation is now resolved/lifted.

David H


On Oct 28, 8:09 am, David Hadiprijanto <david.hadiprija...@gmail.com>
wrote:

Daniel Cazzulino

unread,
Oct 29, 2009, 6:32:54 PM10/29/09
to moq...@googlegroups.com
The answer in stackoverflow is a good one :)
 
Yes, we have no way of matching lambdas for equality. I had a thread with someone at Microsoft about it, and graph comparison is of course a tricky business. So we don't do it ;)
 
The comparison with ToString works most of the time, though.

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


José F. Romaniello

unread,
Oct 30, 2009, 8:27:10 AM10/30/09
to Moq Discussions
Instead of compare lambdas, I do this:

http://jfromaniello.blogspot.com/2009/10/howto-mock-linq-based-dao.html

Although now I'm not using moq for these kind of tests now.


On Oct 29, 7:32 pm, Daniel Cazzulino <k...@clariusconsulting.net>
wrote:
> The answer in stackoverflow is a good one :)
>
> Yes, we have no way of matching lambdas for equality. I had a thread with
> someone at Microsoft about it, and graph comparison is of course a tricky
> business. So we don't do it ;)
>
> The comparison with ToString works most of the time, though.
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>
> On Thu, Oct 29, 2009 at 17:25, David Hadiprijanto <
>
>
>
> david.hadiprija...@gmail.com> wrote:
>
> > For what is worth, I think this is a limitation in Moq.
>
> > I found this link (http://groups.google.com/group/moqdisc/
> > browse_thread/thread/20f5a2ceefcba0e/8f477c1f5c4b5321?
> > lnk=gst&q=func#8f477c1f5c4b5321<http://groups.google.com/group/moqdisc/browse_thread/thread/20f5a2cee...>
> > )

David Hadiprijanto

unread,
Oct 30, 2009, 9:39:26 AM10/30/09
to Moq Discussions

Thanks Daniel.

I also would like to use this opportunity to say thanks for developing
Moq and making it available to all of us. I do like the approach of
'less' as a feature in Moq - making adopting Unit Test a much better
experience, at least for me personally.

Thank you!

David H

On Oct 29, 3:32 pm, Daniel Cazzulino <k...@clariusconsulting.net>
wrote:
> The answer in stackoverflow is a good one :)
>
> Yes, we have no way of matching lambdas for equality. I had a thread with
> someone at Microsoft about it, and graph comparison is of course a tricky
> business. So we don't do it ;)
>
> The comparison with ToString works most of the time, though.
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>
> On Thu, Oct 29, 2009 at 17:25, David Hadiprijanto <
>
> david.hadiprija...@gmail.com> wrote:
>
> > For what is worth, I think this is a limitation in Moq.
>
> > I found this link (http://groups.google.com/group/moqdisc/
> > browse_thread/thread/20f5a2ceefcba0e/8f477c1f5c4b5321?
> > lnk=gst&q=func#8f477c1f5c4b5321<http://groups.google.com/group/moqdisc/browse_thread/thread/20f5a2cee...>
> > )

Daniel Cazzulino

unread,
Oct 30, 2009, 11:14:21 PM10/30/09
to moq...@googlegroups.com
Thanks a lot!


/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


Reply all
Reply to author
Forward
0 new messages