我有一个单元测试在违反约束的情况下失败了,而这是不应该的。我正在测试是否将有效的枚举设置为域类的变量。
我的枚举:
public enum GenderPreference {
M('Male'),
F('Female'),
A('Any')
final String value
GenderPreference(String value) {
this.value = value
}
public String toString() {
value
}
public String getKey() {
name()
}
public String getValue() {
value
}
}
我的域名:
class Profile {
GenderPreference genderPreference
static constraints = {
genderPreference (blank:true, nullable:true)
}
}
我的单元测试:
工作正常:
def instance = new Profile(genderPreference: GenderPreference.M)
assertTrue instance.validate(['genderPreference'])
这应该会失败,但没有。QQQ不是有效的枚举
instance = new Profile(genderPreference: 'QQQ')
assertFalse instance.validate(['genderPreference'])
我使用的是grails 2.2.4。我认为在旧版本中,必须在枚举的域约束中添加一些其他东西,但我认为这应该可以工作。遗漏了什么?
发布于 2013-11-11 18:50:17
可以使用inList将约束添加到枚举
static constraints = {
genderPreference blank:true, nullable:true,
inList: GenderPreference.values() as List
}
https://stackoverflow.com/questions/19912983
复制相似问题