【问题标题】:Rake cannot load such file Unit TestRake 无法加载此类文件单元测试
【发布时间】:2019-03-13 16:26:44
【问题描述】:

我想用 rake 自动化测试。 当我从命令提示符处运行它时,在主目录中,我的测试运行良好。

ruby -I  lib/radar_mon/ tests/radar_mon/test_geoname.rb

我的 Rakefile 看起来像这样:

task default: %w[test]

task :test do
   ruby "tests/radar_mon/test_geoname.rb"
end

我的测试文件 test_geoname.rb 看起来像这样

require 'test/unit'
require 'geoname'

class TestGeoname < Test::Unit::TestCase
   def test_first
      g = Geoname.new( [0.0,0.0] )
      assert_equal( [0.0,0.0], g.location)
   end
end

库结构是这样的

home/lib/radar_mon/geoname.rb

home/tests/radar_mon/test_geoname.rb

home/Rakefile

如何配置我的 rake 文件来运行测试?我不应该更改测试文件中的 require 语句。

/home/pi/.rvm/rubies/ruby-2.2.10/bin/ruby tests/radar_mon/test_geoname.rb
tests/radar_mon/test_geoname.rb:2:in `require_relative': cannot load such file -- /home/pi/Documents/HurricaneTracker/tests/radar_mon/geoname (LoadError)
        from tests/radar_mon/test_geoname.rb:2:in `<main>'
rake aborted!

不确定是否重要,但我正在使用 rvm。

【问题讨论】:

  • 编辑您的帖子并粘贴实际的错误消息。
  • 您是否尝试在 Rakefile 中添加要求?
  • 不,我没试过

标签: ruby unit-testing rake rakefile


【解决方案1】:

如果尝试在 Rakefile 的顶部添加类似的内容,您可能需要将其修改为仅需要 rake 任务所需的文件。

Dir[File.join(__dir__, "/**/*.rb")].each do |file|
  require file
end

更具体地说,对于您的情况,这将起作用:

my_files = FileList['home/lib/radar_mon/*.rb', 'home/tests/radar_mon.*rb']

my_files.each do |file|
  require file
end

task default: %w[test]

task :test do
  ruby "tests/radar_mon/test_geoname.rb"
end

这是可行的,因为 Rake 在这种情况下递归地处理需求。 FileListrake 提供。

有关更多信息,您可能想参考rake gem documentation 或许还有http://www.virtuouscode.com/2014/04/24/rake-part-4-pathmap/

【讨论】:

  • 第二部分不起作用,不确定第一部分是做什么的。
  • 是的,我意识到并删除了它。但第一部分或变体应该可以工作
  • 哇,解决了。你能告诉我为什么吗?
  • 更新了我的答案,但基本上require 只会在尚未加载的情况下加载 ruby​​ 文件。在 Rakefile 中执行 require 应该需要您的任务所需的任何库。
猜你喜欢
  • 2015-03-20
  • 2014-09-15
  • 2015-04-15
  • 2021-04-08
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
相关资源
最近更新 更多