I’ve faced a problem with unit testing web flow controllers
There’s an existings WebFlowUnitTestMixin for unit testing webflow in GDK, but no word on how to use it in the latest Grails 2.2.0 official documentation.
I've used spock framework for my web flow controller unit test. Here is a pretty simple example:
/* SignUpControllerSpec.groovy */
@TestMixin(WebFlowUnitTestMixin)
@TestFor(SignUpController)
class SignUpControllerSpec extends Specification {
def "test empty signup form submission"() {
when: "user loads page with registration form"
registerFlow.start.action()
then: "empty userCommand object is created and lastEvent is start"
lastEventName == 'start'
flow.userCommand instanceof UserCommand
when: "user submits the form with empty lines"
registerFlow.signUp.on.submit.action()
then: "userCommand with errors is returned"
flow.userCommand.hasErrors()
lastEventName == 'signUp'
lastTransitionName == 'submit'
}
}
/* SignUpController.groovy */
class SignUpController {
def userService
def registerFlow = {
start {
action {
flow.userCommand = new UserCommand()
}
on('success').to('signUp')
}
signUp {
on('submit') {
bindData(flow.userCommand, params)
def cmd = flow.userCommand
if (!cmd.validate()) {
return error()
}
// TODO: process valid userCommand
}.to('end')
}
end {
redirect(uri: '/')
}
}
}
Code language: PHP (php)
All of the default unit test controller properties are available + flow, conversation, lastTransitionName, lastEventName, currentEvent.
Have fun!