【问题标题】:Rspec+Capybara - it doesn't make any record changes?Rspec+Capybara - 它不做任何记录更改?
【发布时间】: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


【解决方案1】:

感谢这里的评论者,他们给了我一个线索。

我最初认为可能是 Capybara 不是为测试 ActiveRecord 交互而设计的(因为有模型和控制器 RSpec 测试),所以我并没有尝试过多地调试场景本身。

现在我通过了测试。

    @student = FactoryGirl.create(:student)
    FactoryGirl.create(:account, :account_holder=>@student)

这行是问题的根源,如果准确地说,创建帐户。

student 声明的我的 FactoryGirl 后来(在关注此规范之前)扩展为

after(:build) {|student| student.account = build(:account, :account_holder =>student )}

所以在这里我在一个@student 上获得了 2 个帐户工厂(在研究 page.html 时,我发现了两个我应该拥有的字段组),因此更改了第一个组,即最后创建的帐户。

但在我的模型学生中:

has_one :account, :as => :account_holder

所以在提交第一个 Account 记录的页面输出数据之后,这是表单上的第二个(我更新了第一个,它从未显示)

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2023-02-07
    • 2014-11-21
    • 1970-01-01
    • 2015-08-12
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多