【问题标题】:Cocoon callbacks aren't fired when called from rspec从 rspec 调用时不会触发 Cocoon 回调
【发布时间】: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


【解决方案1】:

不确定您是否特别将其配置为如此,但测试取决于已编译资产的存在。

或者,您曾经编译过资产,然后又忘记了,所以application.js 不包含最新版本,或者您从未编译过资产。

我只是通过编辑config/environments/test.rb 文件并添加来禁用测试中的编译资产

config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true

然后您的测试似乎按预期工作(两个测试仍然失败:删除研究笔记和删除 pericopes,但我猜那是另一回事)。

注意:我还必须编辑你的种子,因为字段 nr_of_verses 在后来的迁移中被重命名为 nrofverses

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-26
    • 2010-10-09
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多