Grails集成测试 - 重定向操作返回null

时间:2014-06-17 13:51:50

标签: unit-testing grails junit integration-testing

这是一个我很难解决的小集成Junit。我重写了几种不同的方法,目前的方法直接来自Grails手册 - 但它仍然会返回null。我没有看到错误;我认为这可能是拼写错误,但我检查了所有这些。我尝试过redirectUrlredirectedUrl - 仍然会返回null

Controller snippet:

@Transactional(readOnly = true)     
def saveReportError() {
    redirect(action:'reportError')  
}

测试:

@Test
void "test save error report"() {
controller.saveReportError()
    assertEquals '/reportSiteErrors/reportError', controller.response.redirectUrl
}

1 个答案:

答案 0 :(得分:1)

我建议将测试作为单元测试来实现。

import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(SimpleController)
class SimpleControllerSpec extends Specification {

    void 'test index'() {
        when:
        controller.index()

        then:
        response.redirectedUrl == '/simple/hello'
    }
}

使用单元测试具有速度的优势。