2014-01-09 21 views
1

我對Capybara和測試工具不太熟練。我正在構建一個多步驟表單或表單嚮導。使用rspec測試多步表單

這是我在我的頭上有一個場景:

  • 用戶訪問註冊路徑
  • 填寫一堆無效數據和一些驗證錯誤應顯示
  • 填寫有效的數據,用戶應採取下一步驟
  • 重複此步驟上述n個步驟與有效數據
  • 檢查,如果用戶的數目增加了1

這是我這麼遠(我好不容易纔得到的東西的工作):

describe "signup" do 
    before { visit signup_path } 
    let(:submit) { 'Start creating my account' } 
    let(:next_btn) { 'Next' } 

    describe "with invalid information" do 
    it "should not create a user" do 
     expect { click_button submit }.not_to change(User, :count) 
    end 

    describe "should not move to the next step" do 
     before { click_button submit } 
     it { should have_content('error') } 
     it { should have_title('Start here') } 
    end 
    end 

    describe "with valid information wizard step 1" do 
    before do 
     fill_in 'First name', with: "Example" 
     fill_in 'Last name', with: "User" 
     fill_in 'email', with: "[email protected]" 
     find("#user_password").set "foobar" 
    end 

    it "should move to the next wizard step 2" do 
     click_button submit 
     should have_content('We need some more info') 
    end 
    it "should have error on the wizard step 2" do 
     fill_in 'Date of birth', with: "Example" 
     click_button next_btn 
     should have_content('error') 
    end 
    end 
end 

此斷言失敗should have error on the wizard step 2,似乎在步驟1中仍然被卡住,我知道這個從看頁面內容和錯誤,其中Dob element can't be foundnext button不能找到。

這是甚至可以用水豚順序測試,保持信息的一步一步?

回答

3

你應該能夠通過嵌套描述塊來進行後續步驟,例如,

describe "signup" do 
... 

    describe "with valid information wizard step 1" do 
    before do 
     fill_in 'First name', with: "Example" 
     fill_in 'Last name', with: "User" 
     fill_in 'email', with: "[email protected]" 
     find("#user_password").set "foobar" 
     click_button submit 
    end 
    it "should move to the next wizard step 2" do 
     should have_content('We need some more info') 
    end 

    describe "and filling out the date of birth incorrectly" do 
     before do 
     fill_in 'Date of birth', with: "Example" 
     click_button next_btn 
     end 

     it "should have error on the wizard step 2" do 
     should have_content('error') 
     end 
    end 
    end 
end 

現在按鈕點擊重複在嵌套塊,所以與第一示例點擊「提交」,以及第二個例子點擊「提交」,然後單擊「next_btn的」等記住該序列每個示例都重複,因此這可能會降低測試速度。 OTOH更準確地反映了用戶的交互,所以權衡可能是值得的。

1

我認爲這不是一個水豚問題,因爲它是一個rspec問題。我已經使用了超過rspec的黃瓜,但據我所知有限的經驗,每個「描述」都是一個獨立的測試案例。我不認爲你也可以。期望這些以任何特定順序執行或b。在測試之間共享上下文/狀態。

同樣,我的經歷主要是在黃瓜身上,但我已經爲這個EXACT場景編寫了測試 - 一個兩頁的用戶註冊嚮導。在黃瓜中,人們寫出「場景」,這些場景基本上是以「給定」,「何時」和「然後」(或其他更糟,假設,行動和斷言)的形式描述用戶與系統的互動的短篇故事。 ),這些都是用簡單的英文寫成的。然後編寫Ruby代碼,將這些英文句子映射到Ruby代碼(可能使用水豚)來測試每個場景描述的內容......我所做的是構建了幾個不同的場景。

  1. 用戶訪問註冊頁面;用一堆錯誤填充它;檢查系統是否顯示錯誤
  2. 用戶訪問註冊頁面;正確填寫它;檢查用戶是否進入第二頁
  3. 用戶已完成第一頁;用一大堆錯誤填充第二頁;檢查系統是否顯示錯誤
  4. 用戶已完成第一頁;正確填寫第二頁;檢查用戶是否確認頁面

很明顯,您可以將這些故事轉換爲rspec。

在這些情況下重現的事情是填寫這兩種形式(無論是好數據還是壞數據)。爲了讓代碼保持乾爽,請創建一些幫助器方法,讓您可以使用值來輕鬆填充表單,這些值將幫助您滿足每種情況。

這有幫助嗎?

+0

嗨肯特,感謝您的迴應我已更改問題的標題,以便您的建議更準確。雖然我明白你想說的是保持代碼乾爽。如果我會先進行測試,然後從第二步跳到第三步,那麼我會這樣做。現在我被困在第二步,因爲你可以看到我的代碼,我無法跳到第三步。你能否提供一個簡單的代碼示例,假設每個表單只有一個必填字段? – ant

+0

不幸的是,我不太清楚rspec是否足夠用於代碼示例。我在一般意義上講的是更多的哲學思考:如何進行嚮導測試。 –

+0

好的,謝謝你的努力 – ant