2009-11-25 65 views
1

只想獲得一些關於如何去測試超過20個必填字段的超長表格的意見。如果我試圖描述需要填充的每個字段(如「我用」blah填寫「Name:」,然後用foo填寫「Address」,那麼看起來像我的黃瓜場景可能會像25行一樣長。等等)使用Rspec/Cucumber測試極長表格的指導,使用Rspec/Cucumber

如果我只是說「當我提供所有必需的信息」作爲黃瓜的一個步驟 - 它看起來有點空,但它保持乾淨,然後使用Factory Girl創建一個工廠來代表一個有效的對象,在黃瓜的步驟,並在型號規格測試此外,我有模型規範,以確保所有必填字段,包括在創建新對象的

問題#1 - 。?這是否足以

問題2 - 如果我有20個以上的必填字段(這個表格收集了很多個人聯繫/歷史信息),你是否在你的rspec模型測試中寫了20+個測試,以確保每個領域都適當考慮?

我知道我被騙了,問兩個問題..;)

回答

2

你要測試的是,當用戶在填寫表單並按下「提交」,該應用程序不會根據提交的信息正確的事, 對?

假設你正在使用標準的黃瓜+ Webrat組合一個Rails應用程序的工作,你可以這樣做:

Background: Login, go to really long form 
    Given the user "Fred exists" # Factory a user into existance, something like @user = Factory(:user, :username => "Fred" 
    And I am on the "Really long form" page # map "Really long form page" in /features/supposrt/paths.rb 

Scenario: Succesfully fill in really long form 
    When I fill in the following: # This is defined in Webrat steps 
    | field 1 | response 1 | 
    | field 2 | response 2 | 
... 
    | field 19 | response 19 | 
    | field 20 | response 20 | 

    And I press "Save" 
    Then I should see a "success" message # assert there is a div with the calss success. Could be an error message 
    And "Fred" should have a valid set of attributes # because you defined @user, you can call whatever assertions you like here i.e. assert equal @user.username, "Fred" (I forget the correct syntax for doing this, but you get the idea). 

這是否幫助?

0

我想你確實希望你的測試能夠實際填寫所有的字段,以確保所有的字段實際上都存在於表單上並且會接受有效的數據。

假設某些字段是必需的,而有些是可選的,我可能會有三個測試:一個填充每個字段,一個填寫只填寫必填字段,另一個填寫必填字段之一。也許第四個測試覆蓋提交沒有任何字段的按鈕,如果它看起來有用的話。這應該保證你不需要太多的字段,並且可能會提醒你改變視圖,如果你稍後改變模型來需要更多的字段或更少的字段,並且確保如果必需的字段可以顯示好的錯誤消息不見了。但是您可以保存模型規格的單個字段要求。