Reputation: 670
I'm trying to get access to saved domain objects during unit testing, so when a controller method saves a domain class outside the scope of the unit test i can access it to test the properties set on it.
have been looking at the domainClassesInfo (DefaultArtefactInfo), savedMetaClasses from interrogating this, but without success.
This seems like something that should be easy -
void testMyControllerMethod() {
mockDomain(MyDomainClass)
controller.myControllerMethod()
//get MyDomainClass instance here for test assertions
//assertEquals value1, myDomainClass.attribute1
}
then in the controller:
def MyControllerMethod() {
//do stuff
MyDomainClass myDomainClass = new MyDomainClass(attribute1:value1,attribute2:value2)
myDomainClass.save()
}
Any thoughts how to extract the saved domain class much appreciated
Upvotes: 0
Views: 480
Reputation: 776
IF the controller is the only one saving a new instance of MyDomainClass then you should be able to do this:
void testMyControllerMethod() {
mockDomain(MyDomainClass)
controller.myControllerMethod()
//get MyDomainClass instance here for test assertions
def result = MyDomainClass.list()[0]
assertEquals value1, result.attribute1
}
Upvotes: 2