views:

24

answers:

2

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?

A: 

"expression" is an Expression>, not a Func<...>... those are two different things. If you do expression.Compile(), I believe it will give you a Func<> (I'm just writing this off the top of my head, so let me know if I'm wrong).

Jon Kruger
A: 

It was something stupid -- .AsQueryable() is the result ... that's why it was complaining it couldn't figure out what kind of where I wanted(?)

        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)));
                    var filtered = items.AsQueryable().Where(expression);
                    var results = new List<TEntity>();
                    filtered.ToList().ForEach(item => results.Add(translator.ToEntity(item)));
                    return results.AsQueryable();
                });
jeriley