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

관련 문제