从 rspec 调用时不会触发 Cocoon 回调

Cocoon callbacks aren't fired when called from rspec

我有一个包含嵌套字段的页面,在开发环境中运行良好。 我使用插入后回调对字段标签进行编号。所以这会创建 "pericoop 1, pericoop 2" 标签。 但是,当我从 rspec 测试页面时,标签没有编号。当我 save_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"

我看过这个 post,300 但是回调根本不起作用。 还有另一个 post(我现在找不到),其中规范不包含 js: true。但这不是这里的问题。

知道这里可能有什么问题吗?

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

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

我只是通过编辑 config/environments/test.rb 文件并添加

来禁用测试中的编译资产
config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true

然后你的测试似乎按预期工作(还有两个测试仍然失败:删除 studynotes 和删除 pericopes 但我猜那是另外一回事)。

Note: I also had to edit your seeds, since a field nr_of_verses was renamed in a later migration to nrofverses.