【问题标题】:Grails integration test - redirect action returns nullGrails 集成测试 - 重定向操作返回 null
【发布时间】:2014-08-07 13:47:23
【问题描述】:

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

控制器sn-p:

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

测试:

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

【问题讨论】:

  • 我还应该注意,我已经尝试了多种方式 Assert 语句中的路径。 '/reportSiteErrors/reportError'、'/reportError' 和 ''
  • 请提供更多详细信息,例如 Grails 版本。为什么要使用集成测试而不是单元测试来测试呢?
  • Grails 2.4.0 -- 我不知道。我是 Grails 的新手,有人告诉我这样做是为了进行集成测试。

标签: unit-testing grails junit integration-testing


【解决方案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'
    }
}

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

【讨论】:

  • 成功了!谢谢你。为什么那行得通而我的junit风格的却行不通?我很好奇。谢谢你的帮助。
  • 不能说。也许你可以发布你所有的测试代码?
  • 只需点击我的答案左侧的接受答案按钮。看看meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多