Moq、翻译器和表达方式

发布于 2024-08-29 01:12:31 字数 1536 浏览 3 评论 0 原文

我正在使用最小起订量“获取服务”中的表达式,并遇到了一个相当烦人的问题。为了让这个测试正确运行并且让获取服务返回它应该返回的内容,中间有一个转换器,它接受您所要求的内容,将其发送出去并获取您真正想要的内容。所以,我认为这很容易,我尝试这样做...... fakelist 是 TEntity 对象(翻译后,由 UI 使用),而 TEnterpriseObject 是实际的持久性。

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;
        });

我在 items.where(expression) 下看到红色波浪线 - 表示无法从使用中推断出它(在 > 之间混淆) >)

一个更简单的版本效果很好......

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

所以我不确定我错过了什么......想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫思念不要吵 2024-09-05 01:12:31

“表达式”是一个Expression>,而不是一个Func<...>...这是两个不同的东西。如果你执行expression.Compile(),我相信它会给你一个 Func<> (我只是凭空写下这个,所以如果我错了请告诉我)。

"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).

手心的温暖 2024-09-05 01:12:31

这是一些愚蠢的事情 - .AsQueryable() 是结果......这就是为什么它抱怨它无法弄清楚我想要什么样的地方(?)

        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();
                });

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();
                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文