2012-04-10 110 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