2

从 迁移jquery_ujs到之后rails-ujs,某些 JS 测试失败并且证明非常难以调试。

问题似乎是 Jquery 对象仅针对序列中的第一个测试进行初始化。在我的开发环境中一切正常。

升级到 Rails 5.1 后一切正常jquery_ujs。然后我只对application.js.

#added this
//= require rails-ujs
# removed this
//= require jquery_ujs

这是一个例子。与 select2 字段交互的两个几乎相同的系统测试。

it "selects recognized search strings", :js do
  expect(MyRemoteSelectBuilder).to receive(:search_query).
    with("recognized_string").once.
    and_return(
      [
        id:   1,
        name: "Recognized Name"
      ]
    )

  visit new_my_object_path 
  # triggers the select2 
  is_expected.to have_selector('.select2-container', visible: false)
  first('.select2-container', minimum: 1).click
  # do some more stuff
end
it "does not select unrecognized search strings", :js do
  expect(MyRemoteSelectBuilder).to receive(:search_query).
    with("unrecognized_string").once.
    and_return(
      [ ]
    )

  visit new_my_object_path 
  # triggers the select2 
  is_expected.to have_selector('.select2-container', visible: false)
  first('.select2-container', minimum: 1).click
  # do some more stuff
end

一起运行时,这些测试之一将失败

expected to find css ".select2-container" but there were no matches  

如果我只专注并运行其中一项或多项测试,它们就会通过。

我考虑过这是否可能是一个 turbolinks 问题(我不认为这是因为测试有时会通过)。尽管如此,我尝试了各种方法来使用 Turbolinks 初始化 Select2,但没有成功。

(($) ->
  $(document).on 'turbolinks:load', ->
    InititializeSelect2.init()
    return
  return
) jQuery

# or 
$ ->  
  InititializeSelect2.init()
# etc

rails-ujs测试之间是否存在或jquery_ujs可能影响 jquery 初始化的根本区别?我是否忽略了一些配置?调试此问题的合乎逻辑的步骤是什么?

4

0 回答 0