1

在我的 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
4

1 回答 1

1

If you're looking to test JavaScript-related functionality, then you need to pass js: true as an option to the related 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

In order to do this, you also need either the selenium-webdriver or capybara-webkit gems. My impression is that Selenium is easier to get started with, but the WebKit one runs much faster.

Note that you can also pass js: true to feature, context, or describe if you need to test several scenarios with JavaScript.

于 2014-06-27T11:36:48.627 回答