在grails 2.2.4下的单元测试中,我尝试传入无效的枚举,看它是否被拒绝。它没有。
这是我的枚举:
public enum CertificationStatus {
N("No"),
Y("Yes - Unverified"),
V("Yes - Verified")
final String value
CertificationStatus(String value) {
this.value = value
}
public String toString() {
value
}
public String getKey() {
name()
}
public String getValue() {
value
}
}
这是我的域名:
class Profile
CertificationStatus certFosterCertified
static constraints = {
certFosterCertified(blank: true, nullable: true)
}
以下是单元测试:
instance = new Profile(certFosterCertified: '#')
assertFalse instance.validate(['certFosterCertified'])
assertNotNull instance.errors.getFieldError('certFosterCertified')
instance.validate返回true,但我在Profile构造函数中为枚举传入了无效值('#')。由于枚举无效,验证失败了吗?枚举的设置仅包含Y,N和V作为有效值。我没想到我必须在约束中设置它们,因为该字段被定义为枚举。
答案 0 :(得分:0)
使用构造函数,您可以将certBackgroundCheck
的值设置为#
。
但是,在您的域类中,您的枚举名为certFosterCertified
。因此certFosterCertified
应为null
,因为它未初始化。根据约束null
是有效状态(nullable: true
)。
也许您只需要将certBackgroundCheck
更改为certFosterCertified
?