【发布时间】:2016-03-31 18:17:40
【问题描述】:
我想知道,当我尝试像更新记录这样测试 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
并且电子邮件已更新。
【问题讨论】:
-
program_stream_path 页面是否真的显示了电子邮件地址?此外,您不应将 eq 与 current_path 一起使用,因为它不会等待页面更改,而应使用 expect(page).to have_current_path(...)
-
@TomWalpole,我添加了打印页面输出。此表应包含更新的电子邮件地址,目前浏览器中会发生这种情况
-
如果您手动运行它会更新吗?你确定你没有遇到验证错误或类似的错误吗?
-
@Doon,所以基本上 Capybara 应该与 activerecord 和数据库交互?
-
在浏览器中它可以工作,不知道在哪里搜索错误...可能是我的 Capybara/Rspec/test 环境配置?
标签: ruby-on-rails rspec capybara