views:

194

answers:

0

I'm new to grails. How do I test a form command object to make sure that it's working? Here's some setup code in a test. When I try to do it, I get the following exceptions:

Error occurred creating command object.
org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Error occurred creating command object.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
....
Caused by: groovy.lang.MissingPropertyException: No such property: password for class: project.user.RegistrationForm
Possible solutions: password

Here is my test case. As you can see, I set "password" on the params map...

void testSaveWhenDataIsCorrect() {
    controller.params.emailAddress = "[email protected]"
    controller.params.password = "secret"
    controller.params.confirmPassword = "secret"

    controller.save()

    assertEquals "success", redirectArgs.view
...
}

Here's the controller action, that adds the command object as a closure parameter:

def save = { RegistrationForm form ->
    if(form.hasErrors()) {
        render view: "create", model: [form: form]
    } else {
        def user = new User(form.properties)
        user.password = form.encryptedPassword
        if(user.save()) {
            redirect(action: "success")
        } else {
            render view: "create", model: [form: form]
        }
    }
}

Here's the command object itself... and note that it DOES have a "password" field...

class RegistrationForm {

    def springSecurityService

    String emailAddress
    String password
    String confirmPassword

    String getEncryptedPassword() {
        springSecurityService.encodePassword(password)
    }

    static constraints = {
        emailAddress(blank: false, email: true)
        password(blank: false, size:4..10)
        confirmPassword(blank: false, validator: {
            password != confirmPassword
        })
    }
}

I'm totally lost in the non-intuitive way to do controllers... Please help.