Thursday, 15 September 2011

Is unit testing of mongodb dynamic attributes possible in Grails 2.2? -



Is unit testing of mongodb dynamic attributes possible in Grails 2.2? -

it seems docs mongodb-1.1.0ga outdated when comes unit testing section: http://springsource.github.com/grails-data-mapping/mongo/manual/ref/testing/datastoreunittestmixin.html

following code

@testfor(employee) class employeetests extends groovytestcase { void setup() { } void teardown() { } void testsomething() { mockdomain(employee) def s = new employee(firstname: "first name", lastname: "last name", occupation: "whatever") s['testfield'] = "testvalue" s.save() assert s.id != null s = employee.get(s.id) assert s != null assert s.firstname == "first name" assert s['testfield'] == "testvalue" } }

fails error:

no such property: testfield class: employee

employee class pretty straightforward:

class employee { string firstname string lastname string occupation static constraints = { firstname blank: false, nullable: false lastname blank: false, nullable: false occupation blank: false, nullable: false } }

so, unit testing of dynamic attributes possible? if is, how?

there's no out of box back upwards dynamic attributes it's easy add. i've set next code in setup method. add together dynamic attributes domain classes have enabled using @testfor or @mock.

grailsapplication.domainclasses.each { domainclass -> domainclass.metaclass.with { dynamicattributes = [:] propertymissing = { string name -> delegate.dynamicattributes[name] } propertymissing = { string name, value -> delegate.dynamicattributes[name] = value } } }

unit-testing mongodb grails gorm

No comments:

Post a Comment