【发布时间】:2014-02-21 23:32:34
【问题描述】:
我目前正在尝试使用 Spock 框架在 Grails 中对域模型的约束进行单元测试,但遇到了一些问题。所以我有以下具有各种约束的域模型:
class profile {
String phoneNo
String userName
static belongsTo = [group:Group]
static constraints = {
phoneNo(blank:false, maxsize:14, matches:"44[0-9]{10}")
}
}
然后我有这个测试应该能够一次测试每个字段约束并返回预期结果:
@Unroll("test profile all constraints #field is #error")
def "test profile all constraints"() {
when:
def newObj = new Profile("$field": val)
then:
validateConstraints(newObj, field, error)
where:
error | field | val
'blank' | 'phoneNo' | '447897654321'
'maxSize' | 'phoneNo' | '123456789012'
'matches' | 'phoneNo' | getPhoneNumber(true)
}
但是,当我运行测试时说电话号码字段的最大大小约束并传递一个小于可用最大大小的值我希望这个测试通过但它失败了,事实上所有的测试都失败了,我不确定为什么因为我是使用这个框架的新手。我非常感谢您对此的帮助。
提前致谢
【问题讨论】:
-
域类中没有
mobileNo字段。测试不应该使用phoneNo作为数据网格中的字段吗? -
是的,对不起,我不得不手动输入代码,这是一个错字。
-
maxsize 应该是 maxSize(注意大写的 S)
标签: unit-testing grails constraints spock