单元测试Grails控制器链接

时间:2014-07-23 11:47:17

标签: unit-testing grails controller chain

我无法弄清楚如何测试“链”的控制器动作。我想验证一下这个动作。

Grails: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似乎为null。

1 个答案:

答案 0 :(得分:3)

你可以这样做......

@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'

    }
}

我希望有所帮助。