Unit testing 在Grails2.2中是否可以对mongodb动态属性进行单元测试?

Unit testing 在Grails2.2中是否可以对mongodb动态属性进行单元测试?,unit-testing,mongodb,grails,gorm,Unit Testing,Mongodb,Grails,Gorm,在单元测试部分,mongodb-1.1.0GA的文档似乎已经过时: 以下代码 @TestFor(Employee) class EmployeeTests extends GroovyTestCase { void setUp() { } void tearDown() { } void testSomething() { mockDomain(Employee) def s = new Employee(first

在单元测试部分,mongodb-1.1.0GA的文档似乎已经过时:

以下代码

@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"

    }
}
由于以下错误而失败:

No such property: testField for class: Employee
Employee类非常简单:

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
    }
}

那么,动态属性的单元测试可能吗?如果是,如何添加?

动态属性没有现成的支持,但添加起来相当容易。我已经在我的设置方法中添加了以下代码。它将向您使用
@TestFor
@Mock
启用的任何域类添加动态属性

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

它是否创造了静态的东西?如果我将其设置为1,则所有项目的值都相同!是的,我错了。使用
for
循环并执行该操作(我在这里写了一篇关于原因的博客),我将更新答案我已经编辑了你的答案!,这对我有用!请随意批准或拒绝我的回答!我想你最好也更新你的博客!我不明白。我已经编辑过了&这个博客包含了正确的信息。这个特殊的解决方案不起作用!当一个实例更改时,该域类的所有实例都将更改!下面是我的问题和解决方案: