我无法弄清楚如何测试执行“链接”的 Controller 操作。我想验证这个 Action 。
chalice :2.4.2
Controller :
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'
}
}
希望对您有所帮助。
关于unit-testing - 单元测试 Grails Controller 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24909675/