Testing controllers with command objects
Since Grails 1.0-RC1 it has been trivial to test controller actions that use command objects. You just supply parameters to the request and it will automatically do the command object work for you when you call your action with no parameters:
Given a controller using a command object:
class AuthenticationController { def signup = { SignupForm form -> ..... }}
You can then test it like this:
def controller = new AuthenticationController()controller.params.login = 'marcpalmer'controller.params.password = 'secret'controller.params.passwordConfirm = 'secret'controller.signup()
Grails automagically sees your call to signup() as a call to the action and populates the command object from the mocked request parameters. During controller testing, the params are mutable with a mocked request supplied by Grails.





















1 Comment
Manohar
November 2, 2007I’m always happy to see improvements in testing. Grails offers so many great features, but the one area that calls for improvement is in testing.
Keep up the good work.