creating a tree-like structure in GORM
I am trying to correctly define a tree structure in GORM and having trouble.
i have one domain object:
class Navigation {
Navigation parent
List children;
String name;
static belongsTo = [parent: Navigation]
static hasMany = [children: Navigation]
static constraints = {
parent(nullable: true);
}
}
and the test:
void testTree() {
Navigation root = new Navigation(name:"root");
Navigation top1 = new Navigation(name:"home");
Navigation top2 = new Navigation(name:"services");
root.addToChildren(top1).addToChildren(top2).save(flush: true);
Navigation s1 = new Navigation(name:"plumbing")
Navigation s2 = new Navigation(name:"baking")
top2.addToChildren(s1).ad开发者_StackOverflow中文版dToChildren(s2).save(flush: true);
Navigation t = Navigation.findByName("root")
assert t.children.size() == 2
}
if i run this test, i get this error:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: grails.Navigation.addToChildren() is applicable for argument types: (grails.Navigation) values: [grails.Navigation : null]
and if i place the contents of that test into boostrap, i dont get that error, and the application starts up, except the navigation table is empty
what can i do to fix this?
You need to mock the Navigation domain so that you can use the GORM methods in your unit test.
Add this to the top of your test case:
mockDomain(Navigation)
精彩评论