rspec看不到茧以嵌套形式动态添加或删除的字段 [英] rspec doesn't see fields dynamically added or removed by cocoon in nested form

查看:123
本文介绍了rspec看不到茧以嵌套形式动态添加或删除的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application in Rails 4, I use Cocoon to have nested forms.

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

It works well in the browser, but I want to add some tests with rspec.

如果我在测试中进行了click_link("add")(或删除")操作,则rspec看不到任何修改(我在打印前后打印page.body).

If I make a click_link("add") (or 'remove') in my test, rspec doesn't see any modification (I print page.body before and after).

我也尝试过使用sleep(10),但这是相同的.

I also tried with a sleep(10) but it's the same.

我该如何测试操作(添加或删除嵌套表单)是否运作良好?

How could I test if the action (add or remove a nested form) works well ?

谢谢

要测试的场景:

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

推荐答案

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

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

为此,您还需要selenium-webdrivercapybara-webkit宝石.我的印象是Selenium较容易上手,但是WebKit的运行速度要快得多.

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.

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

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

这篇关于rspec看不到茧以嵌套形式动态添加或删除的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆