【问题标题】:ActionView::Template::Error: 785: unexpected token at ''ActionView::Template::Error: 785: '' 处出现意外标记
【发布时间】:2020-02-25 12:14:50
【问题描述】:

此错误在运行rails test 后的测试(标准 Rails 测试工具包)期间随机发生。失败的测试可以是任何文件中的任何测试。

此错误通常发生在 CI/CD 过程中的测试环境中。在本地很少发生。

这是完整的堆栈跟踪:

Error:
SitePositionsControllerTest#test_should_create_position_link:
ActionView::Template::Error: 785: unexpected token at ''
    app/views/layouts/site/application.html.erb:10
    test/controllers/site_positions_controller_test.rb:28:in `block (2 levels) in <class:SitePositionsControllerTest>'
    test/controllers/site_positions_controller_test.rb:27:in `block in <class:SitePositionsControllerTest>'
    test/test_helper.rb:64:in `block (3 levels) in run'
    test/test_helper.rb:63:in `block (2 levels) in run'
    test/test_helper.rb:62:in `block in run'
    test/test_helper.rb:54:in `run'

rails test test/controllers/site_positions_controller_test.rb:18

【问题讨论】:

  • 我以前也遇到过,其实你的代码没有问题。您可以删除该测试部分。
  • 大多数时候我得到的错误与 json 解析或格式错误的 javascript 有关。第 10 行的应用程序布局是什么?
  • @arieljuod 是这一行:&lt;link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"&gt;
  • 我也在 CI/CD 中随机获得这个。那行 app/views/layouts/site/application.html.erb:10 通常指向 javascript_pack_tag 语句,这让我相信测试和 webpack 之间存在某种竞争条件
  • 此行为在 rails/webpacker 发布,问题仍未解决:github.com/rails/webpacker/issues/2860

标签: ruby-on-rails ruby continuous-integration ruby-on-rails-6 webpacker


【解决方案1】:

就像@jellymann 一样,我在拨打javascript_pack_tag 时遇到了这个问题。

我在rails test 之前添加了bin/rails webpacker:compile,它似乎已经解决了这个问题。

这种情况经常发生。在我进行该更改后的两天内,至少有十几个构建版本没有出现此错误。

【讨论】:

  • 在 CI 中也看到了同样的问题。在运行测试之前,我的工作流程中已经有rake webpacker:compile。仅在某些运行中看到失败。虽然我认为编译在测试开始之前就完成了,但@jellymann 是正确的,偶尔的失败感觉就像这些操作之间的某种竞争条件。
  • 是的。这对我来说听起来也像是一种竞争条件。 FWIW,对其他人来说,自从我进行了上述更改后,我再也没有看到这一点。 ?‍♂️
【解决方案2】:

我在使用 webmock 和 VCR 时遇到了同样的问题。

解决了添加这个初始化程序(一些hints

# config/initializers/webmock.rb
if Rails.env.test?
  require 'webmock'
  WebMock.disable_net_connect!(allow_localhost: true)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2012-01-16
    相关资源
    最近更新 更多