【问题标题】:Grails 2 'order' criteria not working in unit testsGrails 2“顺序”标准在单元测试中不起作用
【发布时间】:2012-10-11 07:52:10
【问题描述】:

我正在使用 grails 2.1.0 和 groovy 1.8。我在服务中有以下代码:

def listServerPartners() {
    ServerModel.withCriteria {
        partner {
            order('name', 'asc')
        }
        projections {
            distinct('partner')
        }
    }
}

这适用于正在运行的应用程序和集成测试。但是,当我尝试使用单元测试时,我得到一个空白列表。

我注意到,如果我将上面的代码更改为下面的代码并在对象伙伴中实现 comparable 接口,单元测试可以工作,但应用程序和集成测试却不能。

def listServerPartners() {
    ServerModel.withCriteria {
        projections {
            distinct('partner')
        }
        order('partner', 'asc')
    }
}

查看运行应用程序时的 SQL,我注意到对于第一个代码,我得到了一个

"order by partner_al1_.PartnerName asc"
,对于第二个,我得到一个
"order by partner_al1_.Partner_Id asc"
,这显然是我不想要的。

关于如何模拟我在为单元测试运行应用程序和集成测试时的正确行为有什么建议吗?

更新:单元测试的输出:

assert Partner.list() == results
               |      |  |
               |      |  []
               |      false
               [Partner 4, Partner 3, Partner 2, Partner 1]

-- 谢谢

【问题讨论】:

  • 仅供参考:这个was a bug 直到 RC2。所以我想知道为什么你仍然有这个问题..
  • ..and, as I found right now,在 2.0.4 版本中也是如此。
  • 问题不在于投影块。我对其他不使用投影的条件查询也有同样的问题。我认为问题在于订单声明本身。可能是模拟实现中的错误?
  • 你用 order('partner.name', 'asc') 试过第二个版本吗?
  • 我刚试过。运行单元测试时,我得到 groovy.lang.MissingPropertyException: No such property: partner.name

标签: unit-testing grails grails-2.0 hibernate-criteria


【解决方案1】:

我更新了 PlasticCriteria 插件。现在它适用于:

def a = new Artist(name: 'Andreas Achenbach').save()
def c = new Artist(name: 'Constance Gordon-Cumming').save()
def b = new Artist(name: 'Botero').save()
new Portrait(artist: a, name: "Clearing Up—Coast of Sicily").save()
new Portrait(artist: c, name: "Indian Life at Mirror Lake").save()
new Portrait(artist: c, name: "Temporary Chimneys and Fire Fountains").save()
new Portrait(artist: b, name: "Botero's Cat").save()
def artistList = Portrait.withCriteria{
    artist{
        order('name', 'asc')
    }
    projections{
        artist{
            distinct('name')
        }
    }
}
assert ['Andreas Achenbach', 'Botero', 'Constance Gordon-Cumming'] == artistList

可以帮助你 https://github.com/fabiooshiro/plastic-criteria

如果我将预测更改为:

projections{ distinct('artist') }

我收到此集成错误:在这种情况下,“ARTIST_ALI1_.NAME”必须在结果列表中

但适用于单元测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多