我在ROR中看到了一个用于测试某个域类的示例:
context "Validations" do
[:city, :zip, :street].each do |attr|
it "must have a #{attr}" do
a = Address.new
a.should_not be_valid
a.errors.on(attr).should_not be_nil
end
end
end
它动态创建测试,不同的值有不同的名称......这很有趣,但是......我可以用spock或jUnit做到吗?
非常感谢
答案 0 :(得分:6)
使用Spock:
class Validations extends Specification {
def "must have a #attr"() {
def a = new Address()
expect:
!a.valid
a.errors.on(attr) != null
where:
attr << ["city", "zip", "street"]
}
}
如果有多个数据变量,表格语法更方便:
...
where:
attr1 | attr2
"city" | ...
"zip" | ...
"street" | ...