Grails Spock integration test redirectedUrl different between localhost test-app and build server test-app

余生长醉 提交于 2019-12-13 00:56:27

问题


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() {
    }
}

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

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?


回答1:


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.

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

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.

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



来源:https://stackoverflow.com/questions/17822623/grails-spock-integration-test-redirectedurl-different-between-localhost-test-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!