I am attempting to be a good TDD citizen as I design an application. I’m using Moq, and I’ve run into a little repository issue.
My repository has a Find method:
public IEnumerable<T> Find(Expression<Func<T, bool>> where)
{
return _objectSet.Where(where);
}
Then I attempt to set up a mock of the repository:
mock.Setup(m => m.Find(c => c.ConferenceID == conferenceID))
.Returns(ConferenceTestObjectContainer.CreateConferences().Where(c => c.ConferenceID == conferenceID).ToList());
The test will work if I test against the mock directly in the test, but if I inject the mock into my production code (an ASP.NET page in this case,) and test the page method, it doesn’t work.
Justin Etheredge addresses the problem in his post here. The issue is that the comparer between the call and the setup can’t handle Expressions so well.
The problem that I’m having is with his comparer function:
public static Expression<Func<T,bool>> AreEqual<T>(Expression<Func<T,bool>> expr)
{
return Match<Expression<Func<T, bool>>>
.Create(t => t.ToString() == expr.ToString());
}
This raises a compiler error:
The type arguments for method ‘Moq.Match.Create(System.Predicate)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.
I understand that the compiler can’t infer the type of “T”, but I’m not clear on how to fix that. The good Mr. Etheredge made this work, but I don’t understand what I’m doing wrong.
TIA.
OK, so this question is going Tumblweed…
For the record, I worked around the problem by deriving a new Entity-specific Repository and added a FindById() method, which works great.