簡體   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