Grails Spock集成测试redirectedUrl在本地主机测试应用程序和构建服务器测试应用程序之间有所不同 [英] Grails Spock integration test redirectedUrl different between localhost test-app and build server test-app

查看:90
本文介绍了Grails Spock集成测试redirectedUrl在本地主机测试应用程序和构建服务器测试应用程序之间有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spock集成测试,看起来像这样:

I have a Spock integration test that looks something like this:

class PriceTierControllerIntegrationSpec extends IntegrationSpec {

    PriceTierController controller

    def setup() {
        controller = new PriceTierController()
    }

    def "applyDiscount() method will redirect user to success view"() {
        when:
        controller.applyDiscount()

        then:
        controller.response.redirectedUrl == '/priceTier/success'
    }

然后在控制器中,逻辑很简单:

Then in the controller, the logic is simply:

class PriceTierController {

    def applyDiscount() {
        redirect action: 'success'
    }

    def success() {
    }
}

当我在本地计算机上运行此Spock测试时,该测试通过.但是,在构建服务器上,出现以下错误:

When I run this Spock test on my local machine, the test passes. However, on the build server, I get the following error:

controller.response.redirectedUrl == '/priceTier/success'
|          |        |             |
|          |        /test/success false
|          |                      8 differences (46% similarity)
|          |                      /(t---)e(st--)/success
|          |                      /(pric)e(Tier)/success
|              org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@dc42543
com.test.PriceTierController@193d6547

由于某种原因,在构建服务器上,Spock测试认为控制器名称是test而不是priceTier,并且测试将失败.这似乎只在Spock集成测试中发生,因为Spock单元测试和一些旧的Grails mixin测试都可以通过.

For some reason, on the build server, the Spock test thinks that the controller name is test instead of priceTier, and the test will fail. This only seems to happen for Spock integration tests, as the Spock unit tests and a few legacy Grails mixin tests all pass fine.

有人知道什么可能导致此问题吗?

Does anybody know what could be causing this problem?

推荐答案

我也遇到了同样的问题,看来这取决于测试框架从测试类的名称中提取控制器名称.

I've also just experienced this same issue, and it seems that it comes down to the test framework extracting the controller name from the the name of the testing class.

惯例是将测试类命名为<controller name>ControllerSpec

The convention is that the test class is named <controller name>ControllerSpec

在上述情况下,测试类应命名为PriceTierControllerSpec,以便测试框架将控制器成功解析为PriceTeir.

In the above case, the test class should be named PriceTierControllerSpec so that the test framework will successfully resolve the controller to PriceTeir.

按照这些准则命名班级似乎可以解决此问题.

Naming the class according to these guidelines seems to fix this problem.

进一步的参考资料可以在这里找到: https://jira.grails.org/browse/GRAILS -10962

Further reference can be found here: https://jira.grails.org/browse/GRAILS-10962

这篇关于Grails Spock集成测试redirectedUrl在本地主机测试应用程序和构建服务器测试应用程序之间有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆