Moq, a translator and an expression

Posted by jeriley on Stack Overflow See other posts from Stack Overflow or by jeriley
Published on 2010-04-14T13:30:06Z Indexed on 2010/04/14 13:33 UTC
Read the original article Hit count: 353

Filed under:
|
|
|

I'm working with an expression within a moq-ed "Get Service" and ran into a rather annoying issue. In order to get this test to run correctly and the get service to return what it should, there's a translator in between that takes what you've asked for, sends it off and gets what you -really- want. So, thinking this was easy I attempt this ... the fakelist is the TEntity objects (translated, used by the UI) and TEnterpriseObject is the actual persistance.

        mockGet.Setup(mock => mock.Get(It.IsAny<Expression<Func<TEnterpriseObject, bool>>>())).Returns(
            (Expression<Func<TEnterpriseObject, bool>> expression) =>
                {
                    var items = new List<TEnterpriseObject>();
                    var translator = (IEntityTranslator<TEntity, TEnterpriseObject>) ObjectFactory.GetInstance(typeof (IEntityTranslator<TEntity, TEnterpriseObject>));
                    fakeList.ForEach(fake => items.Add(translator.ToEnterpriseObject(fake)));
                    items = items.Where(expression);
                    var result = new List<TEnterpriseObject>(items);
                    fakeList.Clear();
                    result.ForEach(item => translator.ToEntity(item));
                    return items;
                });

I'm getting the red squigglie under there items.where(expression) -- says it can't be infered from usage (confused between <Func<TEnterpriseObject,bool>> and <Func<TEnterpriseObject,int,bool>>)

A far simpler version works great ...

        mockGet.Setup(mock => mock.Get(It.IsAny<Expression<Func<TEntity, bool>>>())).Returns(
            (Expression<Func<TEntity, bool>> expression) => fakeList.AsQueryable().Where(expression));

so I'm not sure what I'm missing... ideas?

© Stack Overflow or respective owner

Related posts about moq

Related posts about generics