2012-03-01 39 views
1

我們需要的是讓一個示例運行,除非它的所有依賴項都已成功運行。如何在rspec示例之間設置一些依賴關係?

即:

describe group_example do 
    it example_1 do 
     #### 
    end 
    it example_2 do 
     #### 
    end 
    it example_3 do 
     #### 
    end 

    example_4 should not run unless example_1, example_2 & example_3 
    Returns Sucess else return NOT RUN 
end 

請您指教該怎麼辦呢?

謝謝。

問候, Nouha

+0

規格/測試必須是獨立的,不要依賴訂單並相應地設計您的規格。 – 2012-03-01 11:31:21

回答

4

這個建議,我認爲你需要調整您的規格 - 而不是規範依賴於其他規格的成功或失敗的早期套件中的運行,可以考慮明確配置你想要的條件測試每個規格。這就是RSpec提供before方法的原因。就目前情況而言,你並沒有真正測試你的應用程序代碼 - 你正在測試你的測試套件的行爲。

在這種特殊情況下,對於第4步,設置應用程序就好像測試1,2和3已成功,然後運行特定於步驟4的測試。最好儘可能隔離待測代碼,如果可以幫助的話,不會在測試之間引入依賴關係。

1

正如@D_Bye所述,您可以使用before來設置和驗證示例組的前提條件,並且可以嵌套示例。

describe "group example" do 
    it "example 1" do 
    #### 
    end 

    it "example 2" do 
    #### 
    end 

    it "example 3" do 
    #### 
    end 

    context "with preconditions" do 
    before(:each) do 
     # Establish the same preconditions as tested by examples 
     # 1, 2, 3, or mark examples in this context as pending. 
     begin 
     #### 
     raise "foo" 
     rescue RuntimeError => e 
     pending "preconditions not met: #{e.message}" 
     end 
    end 

    it "example 4" do 
     #### 
    end 
    end 
end 

您也可以filtering只運行,可以運行這些例子(取決於安裝的軟件,例如)。

+1

互聯網上的第一個答案並沒有暗示某些東西在規格結構上是錯誤的。謝謝! – 2016-05-02 11:49:49