Ruby 当需要env.rb内的文件时,如何消除已初始化的持续警告?

Ruby 当需要env.rb内的文件时,如何消除已初始化的持续警告?,ruby,cucumber,Ruby,Cucumber,我需要features/support/env.rb中的一些文件,如下所示: ['/helpers/*', '/pages/*', '/models/*'].each do |path| Dir[File.dirname(__FILE__) + path].each { |file| require file } end (我这样做是因为我想在运行任何测试之前创建测试用户和其他东西。) 但Cucumber似乎随后使用load加载了这些文件,因为我收到了大量警告,比如Cucumber加载这些

我需要
features/support/env.rb
中的一些文件,如下所示:

['/helpers/*', '/pages/*', '/models/*'].each do |path|
  Dir[File.dirname(__FILE__) + path].each { |file| require file }
end
(我这样做是因为我想在运行任何测试之前创建测试用户和其他东西。)

但Cucumber似乎随后使用
load
加载了这些文件,因为我收到了大量警告,比如Cucumber加载这些文件时:

/home/andrey/dev/project/features/support/models/my_class.rb:2: warning: already initialized constant MyClass::MY_CONSTANT

当场景开始时。如何删除这些警告?

您可以将代码包装在一个
静默\u warnings
块中:

silence_warnings do
  ['/helpers/*', '/pages/*', '/models/*'].each do |path|
    Dir[File.dirname(__FILE__) + path].each { |file| require file }
  end
end

可能有更好的方法来实现您正在尝试做的任何事情,这将很好地发挥您的测试框架的作用,但是上面的代码应该能够处理您的直接问题。

您可能可以在挂接之前在cucumber中设置帮助程序和模型

建议只运行一次before钩子的方法是使用全局变量,因此:

Before do 
  if !$already_required
    ['/helpers/*', '/pages/*', '/models/*'].each do |path|
      Dir[File.dirname(__FILE__) + path].each { |file| require file }
    end
    $already_required = true 
  end 
end 

()

要求
只应加载文件一次。我认为问题出在其他地方。@SergioTulentsev Cumber,而不是
require
也许你可以在hook之前用Cumber中的助手和模特做你想做的事?@Alexis No,安装时间很长,所以我不想在每个场景之前进行安装/feature@AndreyBotalov?问题和答案中的代码需要第一次使用文件,所以沉默警告不会有帮助。第二次使用Cucumber本身加载它们时,会生成警告