Unit testing 在使用Grails2.5.1的Spock单元测试中,控制器始终为空

Unit testing 在使用Grails2.5.1的Spock单元测试中,控制器始终为空,unit-testing,grails,spock,Unit Testing,Grails,Spock,我刚刚使用Grails2.5.1。我需要运行一些单元和集成测试,但我不能让它们工作 我的域类是: class Role { String roleName Role(String _roleName) { roleName = _roleName } static constraints = { roleName(blank: false, nullable: false) } String toString(){ "$roleName" } } class R

我刚刚使用Grails2.5.1。我需要运行一些单元和集成测试,但我不能让它们工作

我的域类是:

class Role {

String roleName

Role(String _roleName) {
    roleName = _roleName
}

static constraints = {
    roleName(blank: false, nullable: false)
}
String toString(){
    "$roleName"
}

}
class RoleController {

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond Role.list(params), model:[roleInstanceCount: Role.count()]
}

def show(Role roleInstance) {
    respond roleInstance
}

def create() {
    respond new Role(params)
}

...
}
我的控制器类是:

class Role {

String roleName

Role(String _roleName) {
    roleName = _roleName
}

static constraints = {
    roleName(blank: false, nullable: false)
}
String toString(){
    "$roleName"
}

}
class RoleController {

static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond Role.list(params), model:[roleInstanceCount: Role.count()]
}

def show(Role roleInstance) {
    respond roleInstance
}

def create() {
    respond new Role(params)
}

...
}
在测试/单元I中,RoleControllerSpec类:

import grails.test.mixin.*
import spock.lang.*

@TestFor(RoleController)
@Mock(Role)
class RoleControllerSpec extends Specification {

def 'index action: 1 role'() {
    setup:
    roleInstance.save()

    expect:
    controller.index() == [roleInstanceList: [roleInstance], roleInstanceTotal: 1]

    where:
    roleInstance = new Role(roleName: "Role1")
}



def "create action"() {
    setup:
    controller.params.roleName = roleName

    when:

    def model = controller.create()

    then:
    model.roleInstance != null
    model.roleInstance.roleName == roleName


    where:
    roleName = "Role1"

}
}
当我使用test app-unit RoleController运行测试时,会出现以下异常:

|Configuring classpath
.
|Environment set to test
....................................
|Running without daemon...
..........................................
|Compiling 1 source files
.
|Running 2 unit tests...

|Running 2 unit tests... 1 of 2
Failure:  |
index action: 1 role(accessmanagement.RoleControllerSpec)
 |
Condition not satisfied:
controller.index() == [roleInstanceList: [roleInstance], roleInstanceTotal: 1]
|          |       |                      |
|          null    false                  Role1
 role(RoleControllerSpec.groovy:17)

|Running 2 unit tests... 2 of 2
Failure:  |
create action(accessmanagement.RoleControllerSpec)
 |
java.lang.NullPointerException: Cannot get property 'roleInstance' on null object
at accessmanagement.RoleControllerSpec.create action(RoleControllerSpec.groovy:34)

|Completed 2 unit tests, 2 failed in 0m 6s
.Tests FAILED 
|
Error |
Forked Grails VM exited with error
在我的测试中,控制器似乎为空。 在第一个测试中,controller.index()为null。在第二个测试中,def model=controller.create()没有创建对象,然后当我尝试访问model.roleInstance时,它无法获取属性

任何想法都将不胜感激。
谢谢

因为您使用的是respond,而不是简单地从控制器返回映射,所以需要检查model属性

def 'index action: 1 role'() {
    setup:
    Role roleInstance = new Role(roleName: "Role1").save()

    when:
    controller.index()

    then:
    model == [roleInstanceList: [roleInstance], roleInstanceTotal: 1]
}

我建议您阅读关于测试控制器的文档

按照您的建议,模型是空的。它给了我model=[roleInstanceCount:0,emptyCollection:[]刷新控制器后尝试刷新save
save(flush:true)
。index()继续为空