通过grails测试在Controller中通过域类访问db中的数据

通过grails测试在Controller中通过域类访问db中的数据,grails,gorm,grails-plugin,grails-domain-class,grails-controller,Grails,Gorm,Grails Plugin,Grails Domain Class,Grails Controller,response.text返回为null。 在本地主机中,它正在返回哈希值。可能: @TestFor(AppPreferencesController) @Mock( [AppPreferences] ) //controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]] class AppPreferencesControllerSpec extends Specification {

response.text返回为null。 在本地主机中,它正在返回哈希值。

可能:

@TestFor(AppPreferencesController)
@Mock( [AppPreferences] )
//controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
class AppPreferencesControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    /*void "test something"() {
        expect:"fix me"
            true == false
    }*/
    void test_for_index()
    {
        when:
            controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
            params.key = 'role_to_feature_map'
            controller.index()
        then:
            1 == 1
            2 == 2
            println response.text
    }
}
个人意见:下面的代码行不可读。在我工作的地方,它永远不会通过代码审查。试试switch语句


def val=value_type=='integer'?preference.value.tobiginter():(value_type='boolean'?(preference.value='true'| preference.value='1'?true:false):(value_type='array'?preference.value.split(','):(value_type='hash'?hash_conv(preference.value):(value_type='json'?new JsonSlurper().parseText(preference.value):preference.value.toString()))

在测试设置过程中是否在某个地方填充了AppPreferences?如果没有,则不会有任何数据,返回null。不,不填充。但我对grails还不熟悉,怎么做?我必须通过从数据库中获取值来完成此操作。您可以通过设置()来完成此操作,例如,新建AppPreferences(field1:'blah')。保存(failOnError:true,flush:true)AppPreferencesControllerSpec.groovy:14处的grails.validation.ValidationException对于FailOneError:True,听起来您用于为域对象设定种子的一个或多个值违反了约束,请尝试类似于
appPrefObj.errors.allErrors.each{println it}
的操作以查看问题所在
@TestFor(AppPreferencesController)
@Mock( [AppPreferences] )
//controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
class AppPreferencesControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    /*void "test something"() {
        expect:"fix me"
            true == false
    }*/
    void test_for_index()
    {
        when:
            controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
            params.key = 'role_to_feature_map'
            controller.index()
        then:
            1 == 1
            2 == 2
            println response.text
    }
}
void test_for_index() {
    when:
        controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
        controller.params.key = 'role_to_feature_map'  <-- the params attached to the controller
        controller.index()
    then:
        1 == 1
        2 == 2
        println response.text
}
given:
    def appPref1 = new AppPreferences("whatever you must set to pass constraints").save(flush:true)
    controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
    controller.params.key = 'role_to_feature_map'

when:
    controller.index()

then:
    1 == 1
    2 == 2
    println response.text