【问题标题】:rspec doesn't see fields dynamically added or removed by cocoon in nested formrspec 看不到 cocoon 以嵌套形式动态添加或删除的字段
【发布时间】:2014-06-26 19:29:38
【问题描述】:

在我的 Rails 4 应用程序中,我使用 Cocoon 来嵌套表单。

它在浏览器中运行良好,但我想用 rspec 添加一些测试。

如果我在测试中创建click_link("add")(或“删除”),rspec 看不到任何修改(我在前后打印page.body)。

我也试过sleep(10),但还是一样。

如何测试操作(添加或删除嵌套表单)是否正常工作?

谢谢

编辑:

要测试的场景:

scenario "nested form" do
  user_sign_in

  contact = FactoryGirl.create(:contact)

  visit new_message_path
  expect(page).to have_text("New message")

  puts page.html

  click_link('Add an action')  # <------ click on the link to add a new nested form

  puts page.html     # <--- the page is the same as before the click

  expect(page).to have_text("New action")

  user_sign_out
end

【问题讨论】:

  • 你能发布你的整个场景吗?您是否将 js: true 作为选项通过了相关测试?
  • @ChrisPeters 谢谢。你是对的,我没有通过js: true 选项。我不知道这个。我会朝这个方向看。再次感谢
  • 是的,您需要 selenium-webdrivercapybara-webkit gem。我的印象是前者更容易上手和运行。
  • @ChrisPeters:谢谢大家!如果您用js: true 写答案,我会接受。
  • 添加了答案。很高兴你把它整理好了。

标签: ruby-on-rails-4 rspec nested-forms cocoon-gem


【解决方案1】:

如果您要测试与 JavaScript 相关的功能,则需要将 js: true 作为选项传递给相关的 scenarios:

scenario "nested form", js: true do
  user_sign_in
  contact = FactoryGirl.create(:contact)

  visit new_message_path
  expect(page).to have_text("New message")

  click_link('Add an action')
  expect(page).to have_text("New action")

  user_sign_out
end

为此,您还需要 selenium-webdrivercapybara-webkit gems。我的印象是 Selenium 更容易上手,但 WebKit 运行得更快。

请注意,如果您需要使用 JavaScript 测试多个 scenarios,也可以将 js: true 传递给 featurecontextdescribe

【讨论】:

  • 谢谢克里斯,正是这样。 js: true 不见了。也谢谢大家的建议,很详细。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多