views:

179

answers:

1

I have a controller that uses a command object in a controller action. When mocking this command object in a grails' controller unit test, the hasErrors() method always returns false, even when I am purposefully violating its constraints. The more baffling thing is that in production, hasErrors() works! So this is just a testing problem.

 def save = { RegistrationForm form ->
  if(form.hasErrors()) {
   // code block never gets executed
  } else {
   // code block always gets executed
  }
 }

In the test itself, I do this:

 mockCommandObject(RegistrationForm)
 def form = new RegistrationForm(emailAddress: "ken.bad@gmail",
  password: "secret", confirmPassword: "wrong")

 controller.save(form)

I am purposefully giving it a bad email address, and I am making sure the password and the confirmPassword properties are different. In this case, hasErrors() should return true... but it doesn't. I don't know how my testing can be any where reliable if such a basic thing does not work :/

Here is the RegistrationForm class, so you can see the constraints I am using:

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, minSize:4, maxSize: 10)
  confirmPassword(blank: false, validator: { confirmPassword, form ->
   confirmPassword == form.password
  })
 }
}
A: 

Have you tried mockForConstraintsTests ?

E.g. something like...

void testSomething() {

    mockForConstraintsTests(RegistrationForm)

    def form = new RegistrationForm(emailAddress: "ken.bad@gmail", password: "secret", confirmPassword: "wrong")

    form.validate()

    assert 1 == form.errors.getErrorCount()
}

Try just testing the RegistrationForm command object first in its own unit test. (gain some confidence that its actually working)

Maybe using the above for the basis of your test will help!?!?

tinny
mockForConstraintsTest() seems to do pretty much what mockCommandObject does. If I include either-or/both in the setup() of the test, your testSomething() seems to work. It only fails when I don't include any of them.Since that's the case, the other tests that are using hasErrors() are still not returning true :(
egervari