Ad-Hockery

ad-hockery: /ad·hok'@r·ee/, n.
Gratuitous assumptions... which lead to the appearance of semi-intelligent behavior but are in fact entirely arbitrary. Jargon File

Mongo dynamic attributes and Grails unit tests

When using Mongo DB with GORM it’s possible to assign to dynamic attributes of a domain class. However, you’ll find that when you write unit tests for code that uses this feature it isn’t supported. It’s easy to emulate, though.

All domain classes that are enabled in a test using the @TestFor or @Mock annotations are available via grailsApplication.domainClasses. This means you can simply add something like this in your setup method:

for (domainClass in grailsApplication.domainClasses) {
    domainClass.metaClass.with {
        dynamicAttributes = [:]
        propertyMissing = { String name ->
            delegate.dynamicAttributes[name]
        }
        propertyMissing = { String name, value ->
            delegate.dynamicAttributes[name] = value
        }
    }
}

This is a pretty simplistic emulation of the real behavior – any changes you make to dynamic attributes will take effect on the "persisted" domain instance even if the save fails. Generally I don’t think it’s likely there would be much ambiguity around that in a test but it’s worth bearing in mind.

Web Statistics