Is unit testing dynamic mongodb attributes possible in Grails 2.2?

The docs for mongodb-1.1.0GA seem to be outdated when it comes to the 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"

    }
}

unable to execute this error:

No such property: testField for class: Employee

The employee class is pretty simple:

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, is unit testing of dynamic attributes possible? If so, how?

+5
source share
1 answer

, . . , , @TestFor @Mock.

grailsApplication.domainClasses.each { domainClass ->
    domainClass.metaClass.with {
        dynamicAttributes = [:]
        propertyMissing = { String name ->
            delegate.dynamicAttributes[name]
        }
        propertyMissing = { String name, value ->
            delegate.dynamicAttributes[name] = value
        }
    }
}
+4

All Articles