Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 907
raaputin Avatar asked Nov 27 '25 09:11

raaputin


1 Answers

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.

like image 112
Jeff Scott Brown Avatar answered Nov 29 '25 22:11

Jeff Scott Brown