繁体   English   中英

单元测试Grails控制器链接

[英]Unit testing Grails controller chaining

我无法弄清楚如何测试执行“链式”操作的控制器。 我想验证操作。

谷物:2.4.2

控制器:

class MyController {

def index() {

}

def doesChain() {
    chain action: 'index', model: [name: "my name"]
}

}

测试:

@TestFor(MyController)
class MyControllerSpec extends Specification {

def setup() {
}

def cleanup() {
}

void "Action doing chain"() {

    when:
    controller.doesChain()

    then:
    controller.chainModel.name == "my name"
    controller.actionName == "someAction" // fails as actionName == null
}

}

测试动作名称没有通过,因为actionName似乎为空。

你可以做这样的事情...

@TestFor(MyController)
class MyControllerSpec extends Specification {

    void "Action doing chain"() {

        when: 'an action invokes the chain method'
        controller.doesChain()

        then: 'the model is as expected'

        // either of these should work, you don't need both...
        controller.chainModel.name == "my name"
        flash.chainModel.name == "my name"

        and: 'the redirect url is as expected'
        response.redirectUrl == '/my/index'

    }
}

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM