我正在将一个相当大的Grails 2.5应用程序升级到Grails 4.0.3。我的域结构中有一些与我的示例类似的内容。
package com.test
class Face {
static hasOne = [mouth: Mouth]
static constraints = {
}
}
package com.test
abstract class Mouth {
Face face
static constraints = {
}
}
package com.test
class BigMouth extends Mouth {
Integer numberOfTeeth
Boolean tonsilsRemoved
}我希望在验证face对象时看到的行为是,它也应该验证嘴巴对象。所以我设置了一个测试控制器来像这样测试
package com.test
class TestController {
def index() {
Face face = new Face()
face.mouth = new BigMouth(
face: face
)
// numberOfTeeth & tonsilsRemoved cannot be null, but are
assert !face.validate()
println 'no failure'
}
}一个测试用例
package com.test
import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification
class FaceSpec extends Specification implements DomainUnitTest<Face> {
def setup() {
}
def cleanup() {
}
void "test hasOneValidation"() {
when:
domain.mouth = new BigMouth(numberOfTeeth: null)
then:
!domain.validate()
}
}但是,我看到的行为是face是有效的,并且它没有传递assert语句。这对我们在2.5版本中是有效的。关于此测试用例的另一个注意事项是,如果我将BigMouth属性移动到嘴巴,并使其成为一个具体的类,则相同的测试将有一个未能通过验证并传递assert语句的face对象。
这是Grails 4.0.3的预期行为吗?如果不是,我是不是做错了什么?
运行Grails 4.0.3、Java 11、Windows 10
发布于 2020-07-15 20:29:34
这似乎是由Grails 4中的this bug引起的。域层次结构试图对父对象应用子级约束,但由于父对象上不存在子级的属性而失败。
https://stackoverflow.com/questions/62805596
复制相似问题