[英]Rspec+Capybara - it doesn't make any record changes?
我想知道,當我嘗試測試像更新記錄的Capybara + Rspec功能時,如果記錄被更新,我無法捕獲?
例如,我有測試:
scenario "updates list" do
@student = FactoryGirl.create(:student)
@stream.students << @student
FactoryGirl.create(:account, :account_holder=>@student)
visit program_stream_path(@program, @stream)
click_link "Редактировать список студентов"
expect(current_path).to eq edit_students_list_stream_path(@stream)
within("#student_0") do
fill_in "Имя", :with => "Имя"
fill_in "Фамилия", :with => "Фамилия"
fill_in "Электронная почта", :with => "randommail@mail.ru"
print page.html
end
#print page.html
click_button "Сохранить изменения"
expect(current_path).to eq program_stream_path(@program, @stream)
expect(page).to have_content "randommail@mail.ru"
end
通過此測試,我可以確保沒有路由錯誤並且處理了操作,但我無法檢查該記錄是否實際更新了?
expect(page).to have_content "randommail@mail.ru"
此行會導致失敗,但正如您所看到的,我使用此值填充了電子郵件字段。 在瀏覽器中,我可以看到記錄已更新。 那么,Capybara不適合測試這部分應用程序?
編輯:我在提交后添加了print page.html
,相關輸出:
<table class="table spacer">
<tr>
<th>Фамилия и имя</th>
<th>Электронная почта</th>
<th>Телефон</th>
</tr>
<tr><td>Surname1 Name1</td>
<td>somemail1@mail.ru</td> #email unchanged
<td>89112223321</td>
</tr>
</table>
瀏覽器操作日志:
{"student":[{"id":"378","account":{"name":"testname","surname":"testsurname","email":"randommail@mail.ru","phone":""}}]}
UPDATE "accounts" SET "password_digest" = $1, "email" = $2, "updated_at" = $3 WHERE "accounts"."id" = $4 1 ms
/Users/slava/RubymineProjects/lms/lib/list.rb:29 COMMIT
電子郵件已更新。
感謝這里的評論者,誰給了我一個線索。
我首先想到的可能是Capybara不是為了測試ActiveRecord交互而設計的(因為有模型和控制器RSpec測試),所以我沒有嘗試非常調試場景本身。
現在我通過測試。
@student = FactoryGirl.create(:student)
FactoryGirl.create(:account, :account_holder=>@student)
這條線是問題的根源,如果准確說話則創建賬戶。
我的FactoryGirl student
宣言后來(在關注此規范之前)擴展了
after(:build) {|student| student.account = build(:account, :account_holder =>student )}
所以在這里我在一個@student
上有兩個帳戶工廠(因為學習page.html我發現了兩個字段組,我應該有一個),因此更改了第一個組,這是最后創建的帳戶。
但在我的模特學生:
has_one :account, :as => :account_holder
因此,在第一個Account
記錄的提交頁面輸出數據之后,這是表單上的第二個(並且我更新了第一個,從未顯示過)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.