【问题标题】:Uninitialized constant error when running ruby specs on linux, not on windows在 linux 上运行 ruby​​ 规范时出现未初始化的常量错误,而不是在 windows 上
【发布时间】:2018-10-23 05:02:43
【问题描述】:

我在一个 ruby​​ 项目中通过 rspec 运行了一系列 selenium/capybara 规范。到目前为止,我一直在 Windows 机器上运行它们而没有问题。现在我正在尝试迁移到 Linux 机器,但我到处都遇到了问题。

在我的每个测试文件都需要的spec_helper.rb 中,我从下面的这一点开始。然而,当我在 Linux 服务器上运行测试时,我得到了几十个未初始化的常量错误。基本上,代码命中的第一个类被声明为未初始化,尽管已在其他地方初始化。

# frozen-string-literal: true

require 'rspec'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'site_prism'

Dir[File.dirname(__FILE__) + '/helpers/*.rb'].each do |helper|
  require helper
end

Dir[File.dirname(__FILE__) + '/page_objects/mixins/*.rb'].each do |page_object|
  require page_object
end
Dir[File.dirname(__FILE__) + '/page_objects/sections/*.rb'].each do |page_object|
  require page_object
end

Dir[File.dirname(__FILE__) + '/page_objects/*/*.rb'].each do |page_object|
  require page_object
end

一个这样的堆栈跟踪:

加载 ./demo_spec.rb 时出错。

Failure/Error: iframe :export_modal, Pages::ExportModal, :xpath, '//*[@id="qPopupWindow"]/iframe'

NameError:
  uninitialized constant Pages::ExportModal
# /dr01/bamboo/xml-data/build-dir/DEVSYS-RSM2FT-JOB1/spec/page_objects/pages/entity_gl_periods.rb:18:in `<class:EntityGlPeriods>'
# /dr01/bamboo/xml-data/build-dir/DEVSYS-RSM2FT-JOB1/spec/page_objects/pages/entity_gl_periods.rb:2:in `<module:Pages>'
# /dr01/bamboo/xml-data/build-dir/DEVSYS-RSM2FT-JOB1/spec/page_objects/pages/entity_gl_periods.rb:1:in `<top (required)>'
# /dr01/bamboo/xml-data/build-dir/DEVSYS-RSM2FT-JOB1/spec/spec_helper.rb:22:in `block in <top (required)>'
# /dr01/bamboo/xml-data/build-dir/DEVSYS-RSM2FT-JOB1/spec/spec_helper.rb:21:in `each'
# /dr01/bamboo/xml-data/build-dir/DEVSYS-RSM2FT-JOB1/spec/spec_helper.rb:21:in `<top (required)>'
# ./demo_spec.rb:1:in `<top (required)>'
No examples found.

【问题讨论】:

  • 请发布堆栈跟踪错误,否则我们在黑暗中拍摄

标签: ruby linux windows rspec


【解决方案1】:

堆栈跟踪告诉您错误位于 spec_helper 的第 21 行

这里某处的错误:

Dir[File.dirname(__FILE__) + '/page_objects/*/*.rb'].each do |page_object|
  require page_object
end

如果您能够进入服务器控制台,请查看它实际返回的内容并与您的 Windows 环境进行比较。

Dir[File.dirname(__FILE__) + '/page_objects/*/*.rb']

您的需求的加载顺序似乎没有正确触发。您尝试读取/加载的目录是否可能存在权限错误?

【讨论】:

  • 所以当我尝试在服务器上的 irb 控制台中加载文件时,我确实遇到了同样的错误。考虑到它在 PC 上运行良好,这真的没有任何意义。至于权限,它们在两个系统上都是相同的。 644.跨度>
  • 你运行bundle install了吗?你在使用捆绑器吗?如果是这样,请尝试在前面加上 bundle exec rspec
  • 对不起,我以为我昨天发表了这条评论。我都做了,并确认我在两台机器上使用了相同版本的所有 gem。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 2011-11-03
  • 1970-01-01
  • 2011-11-22
相关资源
最近更新 更多