如何在带有模拟域的grails单元测试中测试默认排序 [英] How do you test default sort in a grails unit test with mocked domains

查看:126
本文介绍了如何在带有模拟域的grails单元测试中测试默认排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以测试在staticMappingBlock中定义的排序'propertyName'?

这可以在整合阶段工作,但不能在我的域具有的单元阶段

  static mapping = {
sort'lastName'
}

void testDefaultSortOrder(){
def agent1 = new CommissionAgent(firstName:'fred',lastName:'b',active:true).save()
def agent2 = new CommissionAgent(firstName:'fred2',姓氏:'a',活动:假).save()

def agents = CommissionAgent.list()
assertEquals'排序顺序错误',agents [0] .lastName,agent2 .lastName
assertEquals'sort order is wrong',agents [1] .lastName,agent1.lastName

}

Grails版本是1.3.1

解决方案

没有任何好方法测试我知道的单元测试中的实际排序。你试图测试的东西实际上是GORM集成中不可或缺的一部分,即在支持排序映射的情况下,在模拟域对象上测试它并不会测试将要运行的实际代码。



你可以在单元测试中做的最接近的事情是查看静态映射对象来声明sort的值设置为你期望的值。



我把一个博客文章前不久就如何询问价值观的常规封闭。这种技术可以用来断言排序顺序设置为你期望的结果:



Foo域对象:

  package com.example 
$ b $ class Foo {

字符串名称

静态映射= {
sortname
}
}

FooTests单元测试:

  package com.example 

导入grails.test。*

class FooTests extends GrailsUnitTestCase {
void testFooSort(){
def mappingValues = ClosureInterrogator.extractValuesFromClosure(Foo.mapping)
assertEqualsname,mappingValues.sort
}
}

ClosureInterrogator类允许您查看闭包的功能:

  package com.example 
$ b $ class ClosureInterrogator {
private Map closureValueMap = [:]

静态映射extractValuesFromClosure(Closure闭包){
def interrogator = new ClosureInterrogator(closure)
return interrogator.closureValueMap
}
$ b $ private ClosureInterrogator(Closure closure){
def oldResolveStrategy = closure.getResolveStrategy()
def oldDelegate = closure.getDelegate()
closure.delegate = this
closure.resolveStrategy = Closure.DELEGATE_FIRST

try {
closure()
} finally {
closure.setDelegate(oldDelegate)
closure.setResolveStrategy(oldResolveStrategy)
}
}

//属性getter
def propertyMissing(String name){
return closureValueMap [name]
}

// property setter
def propertyMissing(String name,value){
如果(args.size()== 1){
c() losureValueMap [name] = args [0]
} else {
closureValueMap [name] = args
}
}
}


Is it possible to test the sort 'propertyName' which is defined in the staticMappingBlock

This works during the integration phase but not during the unit phase where my domain has

static mapping = {
    sort 'lastName'
}

void testDefaultSortOrder(){
    def agent1 = new CommissionAgent(firstName: 'fred', lastName: 'b', active:true).save()
    def agent2 = new CommissionAgent(firstName: 'fred2', lastName:'a', active:false).save()

    def agents = CommissionAgent.list()
    assertEquals 'sort order is wrong', agents[0].lastName, agent2.lastName
    assertEquals 'sort order is wrong', agents[1].lastName, agent1.lastName

}

Grails version is 1.3.1

解决方案

There isn't any good way to test the actual sorting in unit tests that I'm aware of. What you're trying to test is really such an integral part of the GORM integration that testing it on mocked domain objects, even if they support the sort mapping, doesn't test the actual code that will be run.

The closest thing that you could do in a unit test would be to take a look at the static mapping object to assert that the value of "sort" is set to what you expect it to be.

I put together a blog post a while ago on how to interrogate groovy closures for values. This technique could be used to assert the sort order is set to what you expect like this:

Foo domain object:

package com.example

class Foo {

    String name

    static mapping = {
           sort "name"
    }
}

FooTests unit test:

package com.example

import grails.test.*

class FooTests extends GrailsUnitTestCase {
    void testFooSort() {
         def mappingValues = ClosureInterrogator.extractValuesFromClosure(Foo.mapping)
         assertEquals "name", mappingValues.sort
    }
}

ClosureInterrogator class that allows you to see what a closure does:

package com.example

class ClosureInterrogator {
    private Map closureValueMap = [:]

    static Map extractValuesFromClosure(Closure closure) {
        def interrogator = new ClosureInterrogator(closure)
        return interrogator.closureValueMap
    }

    private ClosureInterrogator(Closure closure) {
        def oldResolveStrategy = closure.getResolveStrategy()
        def oldDelegate = closure.getDelegate()
        closure.delegate = this
        closure.resolveStrategy = Closure.DELEGATE_FIRST

        try {
            closure()
        } finally {        
            closure.setDelegate(oldDelegate)
            closure.setResolveStrategy(oldResolveStrategy)
        }
    }

    // property getter
    def propertyMissing(String name) {
        return closureValueMap[name]
    }

    // property setter
    def propertyMissing(String name, value) {
        closureValueMap[name] = value
    }

    def methodMissing(String name, args) {
        if (args.size() == 1) {
            closureValueMap[name] = args[0]
        } else {
            closureValueMap[name] = args
        }
    }
}

这篇关于如何在带有模拟域的grails单元测试中测试默认排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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