2016-04-25 140 views
1

我有一個功能測試試圖驗證編輯操作。這是一個超級簡單的測試,但我沒有寫很多,我犯了一些我弄不明白的錯誤。基本上,當頁面被訪問時,它不會呈現要編輯的文本信息,並且「更新」按鈕不存在。這是我的錯誤和代碼清晰。Rspec功能測試混淆

TEST

scenario "Staff can edit the response messages" do 
    group = Group.create!(name: "Group A", response: "You are now subscribed for updates") 
    visit "/groups/#{group.id}/edit" 

    user = FactoryGirl.create(:user) 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 

    expect(page).to have_content("You are now subscribed for updates") 
end 

enter image description here

VIEW

<div class="container text-center"> 
    <div class="row"> 
    <div class="col-lg-8 col-lg-offset-2 well"> 
    <%= form_for @group do |form| %> 
     <div class="form-group"> 
     <%= form.label :body, "Edit The Response:", class: "message_label"%> 
     <%= form.text_field :response, class: "form-control", placeholder: "New things are happening!" %> 
     </div> 
     <%= form.submit "Update", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
</div> 
</div> 

當我運行 「save_and_open_page」 我期待的內容存在更令人奇怪的是什麼嗎?但是,我的測試仍然失敗,說預期的內容不顯示WEIRD!

enter image description here

+0

您好的

group = Group.create!(name: "Group A", response: "You are now subscribed for updates") user = FactoryGirl.create(:user) visit "/groups/#{group.id}/edit" // <= check any static text to ensure the page is loaded // expect will wait for a defailt timeout ~ 2 seconds expect(page).to have_content("Tulim time text") fill_in "Email", with: user.email // ... 

例。在你看來,我沒有看到你再次測試的文本應該顯示在哪裏。帶有失敗消息的圖像不會顯示頁面上實際存在的所有文本(它的右側截斷)通常,我建議使用save_and_open_page或save_and_open_screenshot(如果您的包中有capybara-screenshot)更好地瞭解頁面實際顯示的內容。 – trueunlessfalse

+0

是的,這更奇怪,因爲如果我使用save_and_open_page,它顯示了我期望在頁面上的內容,但是如果我運行測試它不會顯示出來? – Cambass

+0

唱完後只插入'暫停3'僅供測試使用 –

回答

1

據我看到的,問題是:

  1. 試運行visit
  2. fill_in預計該頁面已加載

第二個期望可能是錯的,你必須等待頁面加載手動或魔術sleep 2或自動使用任何capybar一個的取景器/預期

您的測試看起來可能是這樣一個方式:手動延遲

visit "/groups/#{group.id}/edit" 
    // wait a few seconds for page loading 
    sleep 5 
    fill_in "Email", with: user.email 
+0

如何添加睡眠2? – Bitwise

+0

你能告訴我一個例子在我現有的代碼 – Bitwise

+0

@CameronBass,我已經更新了答案 – andrykonchin