控制器在使用 Grails 2.5.1 的 Spock 单元测试中始终为空

Controller always null in Spock unit test using Grails 2.5.1

我是使用 Grails 2.5.1 的新手。我需要运行一些单元和集成测试,但我无法让它们工作。

我的域类是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Role {

String roleName

Role(String _roleName) {
    roleName = _roleName
}

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

}

我的控制器类是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)
}

...
}

在测试/单元中我有类 RoleControllerSpec:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 运行测试时,它给了我以下异常:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|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() 为空。在第二个测试中 def model = controller.create() 没有创建对象,然后当我尝试访问 model.roleInstance 时它无法获取属性。

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


由于您使用的是响应而不是简单地从控制器返回地图,因此您需要检查模型属性

1
2
3
4
5
6
7
8
9
10
def 'index action: 1 role'() {
    setup:
    Role roleInstance = new Role(roleName:"Role1").save()

    when:
    controller.index()

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

我建议您阅读有关测试控制器的文档 https://grails.github.io/grails-doc/2.5.x/guide/testing.html#unitTestingControllers