简体   繁体   中英

Unit testing Grails controller chaining

I am unable to figure out how to test for a controller action that does 'chain'. I would like to verify the action.

Grails: 2.4.2

Controller:

class MyController {

def index() {

}

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

}

Test:

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

}

Testing the action name is not passing as the actionName appears to be null.

You could do something like this...

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

    }
}

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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