Unit Testing nested command objects in grails -


docs can test controllers command objects mocking params http://grails.org/doc/latest/guide/testing.html#unittestingcontrollers

i wonder if works nested command objects? has gotten work?

example:

controller

def create(formcommand form){   form.validate()   ... } 

command

class formcommand {   innercommand cmd }  class innercommand{   string x   static constraints ={      x(nullable: false)   } } 

test

void testcreate(){   params["inner.x"]="any"   controller.create()   ... } 

my expectation command objects created , data binding works, expect inner command validated. expecting much?

ok, seems that's possible want, needs code :-)

data binding

for nested command objects grails databinding needs not null instance of inner command.

to that, can create custom org.codehaus.groovy.grails.web.binding.bindeventlistener:

class innercommandbindeventlistener împlements bindeventlistener {   public void dobind(object target, mutablepropertyvalues source, typeconverter typeconverter) {     target.cmd = new innercommand()   } } 

and declare in resources.groovy

innercommandbindeventlistener(innercommandbindeventlistener) 

nested validation

to resolve validation issue, need custom validator cmd:

class formcommand {   innercommand cmd   static constraints = {     cmd nullable: false, validator: { cmd, obj ->       // manually trigger inner command validation       if(!cmd.validate()) {         return 'invalid.innercommand.message'        }     }   } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -