【发布时间】:2018-02-07 19:32:58
【问题描述】:
我有一个在开发环境中运行良好的嵌套字段页面。 我使用插入后回调对字段标签进行编号。所以这会创建“pericoop 1,pericoop 2”标签。 但是,当我从 rspec 测试页面时,标签没有编号。当我保存_and_open_page 时,我只看到文本“pericoop”。 因此,无论出于何种原因,似乎都没有触发回调。
这是我的 rspec:
scenario 'to multiple pericopes with valid attributes', js: true do
fill_in 'pericoop 1', with: 'Jona 1:1 - 1:10'
click_on 'Voeg nog een pericoop toe'
should_see 'pericoop 2'
fill_in 'pericoop 2', with: 'Jona 2:20 - 3:3'
submit_form
should_see 'Jona 1:1 - 10 | Jona 2:20 - 3:3'
end
还有咖啡脚本
$ ->
$("#pericopes").on "cocoon:after-insert", (event, added_item) ->
num = $("#pericopes div.nested-fields").length
added_item.find('.control-label').html('pericoop '+num)
console.log('pericope numbered')
从测试运行时控制台日志不显示任何内容(我已将 Firefox 配置为保留日志)。
形式:
= simple_form_for studynote, validate: true do |f|
#pericopes
= f.simple_fields_for :pericopes do |ff|
.nested-fields
= ff.input :name,
label: "#{t('simple_form.labels.pericopes.name')} 1",
placeholder: 'Genesis 1:1-3:21',
autofocus: true
= link_to_remove_association 'remove pericope', ff
.links
= link_to_add_association t('add_pericope'), f, :pericopes
= f.input :title
= f.input :note, :input_html => { :rows => 20 }
= f.button :submit, class: "btn-primary"
还有部分:
.nested-fields
= f.input :name,
label: "#{ t('simple_form.labels.pericopes.name') }",
placeholder: 'Genesis 1:1-3:21',
autofocus: true
= link_to_remove_association 'remove pericope', f
编辑,我已经重构了表单和部分代码以干掉一些代码,希望这可以解决问题,但还没有运气。 部分_pericope_fields.html.haml
.nested-fields
- index = index
= f.input :name,
label: "#{ t('simple_form.labels.pericopes.name') } #{index}",
placeholder: 'Genesis 1:1-3:21',
autofocus: true
= link_to_remove_association 'remove pericope', f
表单,_form.html.haml
= simple_form_for studynote, validate: true do |f|
#pericopes
= f.simple_fields_for :pericopes do |pericope|
= render 'pericope_fields', f: pericope, index: 1
.links
= link_to_add_association t('add_pericope'), f, :pericopes, class: 'new btn-xs'
= f.input :title
= f.input :note, :input_html => { :rows => 20 }
= f.button :submit, class: "btn-primary"
我看过这篇帖子,300,但那里的回调根本不起作用。 还有另一篇文章(我现在找不到),其中规范不包含 js:true。但这不是这里的问题。
你有什么想法吗?
【问题讨论】:
-
为了清楚起见:它在浏览器中有效,只是在您的测试中无效,对吧?您认为它不起作用的唯一原因是它没有登录控制台。你能让js在html中产生效果并测试吗? (因为我不确定控制台日志记录是否总是正确捕获)。您使用的是哪个 javascript 驱动程序?显然,在功能测试中测试控制台登录有点奇怪,该功能测试被认为是关于黑盒测试:)(用户看到的)(所以不确定是否会被支持)。
-
是的,它在浏览器中有效,在测试中无效。我没有测试控制台日志,只是出于调试目的添加了它。我的测试断言应该存在文本“pericoop 2”(should_see 'pericoop 2'),这就是测试失败的地方。当我在断言之前添加 save_and_open_page 时,我也可以看到失败。我使用 selenium-webdriver 作为驱动程序。
-
好的。奇怪的。所以要么javascript没有运行(??),要么没有点击链接。那可能吗?你通过文字找到链接,所以标签上的文字可能不一样。例如。测试中的标准语言环境不同或翻译已更改?
-
我确定链接被点击了,因为页面上显示了一个新字段,只是标签错误(没有索引)。这就是为什么我还假设 javascript 没有运行。
-
好的。越来越诡异。点击链接也会触发javascript,否则不会插入字段(cocoon使用javascript插入新项)。
标签: ruby-on-rails ruby rspec apache-cocoon