How to mock/stub calls to message taglib in Grails controller
- by Dave
I've got a Grails controller which relies on the message taglib to resolve an i18n message:
class TokenController {
def passwordReset = {
def token = DatedToken.findById(params.id);
if (!isValidToken(token, params)) {
flash.message = message(code: "forgotPassword.reset.invalidToken")
redirect controller: 'forgotPassword', action: 'index'
return
}
render view:'/forgotPassword/reset', model: [token: token.token]
}
}
I've written a unit test for the controller:
class TokenControllerTests extends ControllerUnitTestCase {
void testPasswordResetInvalidTokenRedirect() {
controller.passwordReset()
assert...
}
}
Since the message taglib is called in the controller I get a MissingMethodException:
groovy.lang.MissingMethodException: No signature of method: TokenController.message() is applicable for argument types: (java.util.LinkedHashMap) values: [[code:forgotPassword.reset.invalidToken]]
Does anyone know the best way to get around this issue in a unit test? Ideally I would like to perform assertions on the message but right now I'd be happy if the test just ran!
Thanks