class A {
String a
}
class B extends A {
String b
}
现在,我想在创建
B
实例时通过 map 构造设置这两个属性 def instance = new B(a: "foo", b: "bar")
assert instance.b != null
只有它不起作用。
实际上,它确实可以在纯Groovy中工作,但不适用于Spock测试中的Grails域对象。
最佳答案
以下测试通过了Grails 2.3.8。
super 类...
// grails-app/domain/inheritedproperties/SuperClass.groovy
package inheritedproperties
class SuperClass {
String a
}
一个子类...
// grails-app/domain/inheritedproperties/SubClass.groovy
package inheritedproperties
class SubClass extends SuperClass {
String b
}
Spock规格...
// test/unit/inheritedproperties/SubClassSpec.groovy
package inheritedproperties
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(SubClass)
@Mock(SuperClass)
class SubClassSpec extends Specification {
void "test binding inherited properties"() {
when:
def instance = new SubClass(a: 'A', b: 'B')
then:
'A' == instance.a
'B' == instance.b
}
}