2012-04-10 106 views
3

我看到ROR一个例子,用于测试一些领域类:我可以用Spock来做这个吗?

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 

它创建具有不同值的飞行测试一个不同的名字......这是一种有趣,但我......能做到这一点与斯波克或jUnit?

非常感谢

回答

6

使用斯波克:

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" | ... 
+0

非常感谢!非常简洁和有用的答案! ;-) 问候 – mpccolorado 2012-04-10 23:13:01