mpc*_*ado 3 junit unit-testing ruby-on-rails spock
我在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
Run Code Online (Sandbox Code Playgroud)
它在运行中创建了不同的值和不同名称的测试...这很有趣,但是......我可以用spock或jUnit做到这一点吗?
非常感谢
使用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"]
}
}
Run Code Online (Sandbox Code Playgroud)
如果有多个数据变量,表语法更方便:
...
where:
attr1 | attr2
"city" | ...
"zip" | ...
"street" | ...
Run Code Online (Sandbox Code Playgroud)