assertEquals controller.action()返回null ....!

时间:2014-06-19 06:54:56

标签: grails integration-testing spock

     class ProjectDashBoardController {
/*
 * This method is used to display project implementation's overview.
 */
def check() {
    render "Hello"
}

这是我的集成测试

  package com.spock

 import grails.plugin.spock.IntegrationSpec

class ProjectDashBoardControllerIntegrationSpec extends IntegrationSpec {

ProjectDashBoardController controller = new ProjectDashBoardController()

def cleanup() {
}

void "test check action"() {

    when:
    controller.check()

    then:
    controller.check()
    assertEquals "/dashboard/index", controller.response.redirectedUrl


}

}

按照,然后条件测试应该失败,因为动作渲染,但它显示这样的错误 没有方法签名:com.itxchg.ProjectDashBoardControllerIntegrationSpec.assertEquals()适用于参数类型:(java.lang.String,null)值:[/ dashboard / index,null]

1 个答案:

答案 0 :(得分:2)

Spock没有assertEquals方法,你应该使用常规的Groovy功率断言,尽管使用spock时assert块中隐含then

 assert "/dashboard/index" == controller.response.redirectedUrl

在您的情况下,虽然您确实有一个重定向方案来测试。实际应该测试的是响应包含渲染文本:

void "test check action"() {
    when:
    controller.check()

    then:
    // Note there is no need to explicitly specify assert in then block
    // Assertion is implicit
    controller.response.text == "Hello"
}