grails:测试域类中的“排序”映射 [英] grails: testing 'sort' mapping in a domain class

查看:156
本文介绍了grails:测试域类中的“排序”映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grails doc 为例:

  class Airport {
...
static hasMany = [flights:Flight]
static mapping = {
航班sort:'number',order:'desc'
}
}

如何测试排序?

解决方案

正如文档中所述,它不会像写入一样工作。您必须将 static belongsTo = [airport:Airport] 添加到Flight。



如果没有belongsTo,会出现以下错误:

关联[Airport-> flights]的默认排序不受单向一对多关系支持。



使用belongsTo,测试可能如下所示:

  class SortSpec extends IntegrationSpec {
deftest grails does sort flights (){
给定:
def airport = new Airport()
airport.addToFlights(new flight(number:A))
airport.addToFlights(new Flight (新号码:B))
airport.save(failOnError:true,flush:true)


def sortedAirport = airport.refresh()//从数据库重新加载以应用排序

然后:
sortedAirport.flights.collect {it.number} == ['C ','B','A']
}
}

但是,编写这样的测试没有多大意义,因为它会检查grails应用排序配置。我为什么要测试Grails?测试你的代码而不是框架。


Given the example from grails doc:

class Airport {
    …
    static hasMany = [flights: Flight]
    static mapping = {
        flights sort: 'number', order: 'desc'
    }
}

How can one test sorting?

解决方案

As stated in the docs, it does not work like written. You have to add static belongsTo = [airport:Airport] to Flight.

Without belongsTo you get the following error:

Default sort for associations [Airport->flights] are not supported with unidirectional one to many relationships.

With belongsTo the test could look like this:

class SortSpec extends IntegrationSpec {
    def "test grails does sort flights" () {
        given:
        def airport = new Airport()
        airport.addToFlights (new Flight (number: "A"))
        airport.addToFlights (new Flight (number: "C"))
        airport.addToFlights (new Flight (number: "B"))
        airport.save (failOnError: true, flush:true)

        when:
        def sortedAirport = airport.refresh() // reload from db to apply sorting

        then:
        sortedAirport.flights.collect { it.number } == ['C', 'B', 'A']
    }
}

But.. it doesn't make much sense to write a test like this because it checks that grails applies the sorting configuration. Why would I want to test grails? Test your code and not the framework.

这篇关于grails:测试域类中的“排序”映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆