2010-03-04 3 views
2

나는 루비 온 레일즈로 했어야을 사용하고, 나는 다음과 같은 테스트 케이스를 가지고 :중복 코드없이 테스트 케이스를 반복하는 방법은 무엇입니까?

class BirdTest < Test::Unit::TestCase 

    context "An eagle" do 
     setup do 
     @eagle = Eagle.new 
     end 
     should "be able to fly" do 
     assert_true @eagle.can_fly? 
     end 
    end 

    context "A Crane" do 
     setup do 
     @crane = Crane.new 
     end 
     should "be able to fly" do 
     assert_true @crane.can_fly? 
     end 
    end 

    context "A Sparrow" do 
     setup do 
     @sparrow = Sparrow.new 
     end 
     should "be able to fly" do 
     assert_true @sparrow.can_fly? 
     end 
    end 

end 

그것은 잘 작동하지만 난 여기에 작성한 중복 코드를 싫어. 그래서 다음과 같은 테스트 케이스를 작성하려고합니다. 이 테스트 케이스는 여러 번 실행해야하며 some_bird의 값이 다른 값으로 설정 될 때마다 실행해야합니다. 그게 가능하니?

class BirdTest < Test::Unit::TestCase 

    context "Birds" do 
     setup do 
     @flying_bird = some_bird 
     end 
     should "be able to fly" do 
     assert_true @flying_bird.can_fly? 
     end 
    end 

end 

감사합니다,

브라이언

답변

2

당신은 현재의 예를

class BirdTest < Test::Unit::TestCase 
    context "Birds" do 
    [Crane, Sparrow, Eagle].each do |bird| 
     context "A #{bird.name}" do 
     should "be able to fly" do 
      this_bird = bird.new 
      assert this_bird.can_fly? 
     end 
     end 
    end 
    end 
end 
+0

환상적인이 뭔가를 시도 할 수 있습니다! 이것은 내가 원하는 것입니다. 고맙습니다! – Shuo