The IsAny<T> is the only way around it now, yes.
APIs that receive expressions are not supported unless you do that. It looked quite complicated to support those, as I didn't have a good way of knowing when to stop "resolving" the expression (as moq itself also receives an Expression!). I think it would be non-trivial.
On Sun, Oct 5, 2008 at 5:45 PM, gd34
<gareth...@googlemail.com> wrote:
I have this method on my repository interface:
interface IRepository
{
IQueryable<TEntity> GetAll<TEntity>(Expression<Func<TEntity,
bool>> predicate) where TEntity : class;
}
and I want to set up an expectation that when I pass in a particular
predicate it returns X.
So I tried:
repositoryMock.Expect(r => r.GetAll<Absence>(a => a.UserId ==
1)).Returns(absences.AsQueryable());
When I do that I get a NotSupportedException. So I tried this to get
around it:
Expression<Func<Absence, bool>> arg = a => a.UserId == 1;
repositoryMock.Expect(r =>
r.GetAll<Absence>(arg)).Returns(absences.AsQueryable());
This works but has no effect on my mock repository. I did a little
test to see why this was and found that this test fails, which
explains why my expectation doesn't work.
Expression<Func<Absence, bool>> arg = a => a.UserId == 1;
Expression<Func<Absence, bool>> arg2 = a => a.UserId == 1;
Assert.AreEqual(arg, arg2); // Fails
I guess the compiler is generating different types so when Moq calls
Equals() it doesn't see the delegates as the same??
I was just wondering if there was:
a) any way around this
b) if anyone knows whether lambda expressions as arguments will be
supported in a future release
c) whether anyone has any idea how hard this might be to implement? I
might try to give implementing it a go myself and submit a patch.
In the end I got around it like this, but this isn't really ideal:
repositoryMock.Expect(r =>
r.GetAll<Absence>(It.IsAny<Expression<Func<Absence,
bool>>>())).Returns(absences.AsQueryable());
I'm using Moq on a fairly simple project at the moment, but if I was
doing something more complicated this work around probably wouldn't be
sufficient!
Any help/suggestions would be appreciated.
Thanks
Gareth