我有这样的测试:
require 'minitest/autorun'
require 'minitest/color'
require_relative '../lib/util/input_file'
class TestInputFile < Minitest::Test
def setup
@input_path = Pathname.new("/path/to/inputs")
end
def test_default_input_file
input_file = Util::InputFile.new(1)
expected_path = @input_path.join('input01.txt')
assert_equal(expected_path, input_file.abspath)
end
# more tests follow
end
对于此代码:
module Util
class InputFile
def initialize(num)
@num = num
@input_dir = Pathname.new("/path/to/inputs")
end
def abspath
basename = 'input'
return @input_dir.join(format('%s%02d.txt', basename, @num))
end
end
end
当我用rake test
运行这个时,一切都如预期的那样通过了;但是,当我从实际的主脚本调用它时,它会被uninitialized constant Util::InputFile::Pathname (NameError)
阻塞,当我在lib/util/input_file.rb
的顶部添加require 'pathname'
时,一切都很好。
为什么单元测试不会以同样的方式失败,我如何重构它,使它失败,除非我在生产代码中有正确的require语句?
**编辑:**Rakefile如下所示:
require 'minitest/test_task'
Minitest::TestTask.create do |t|
t.test_globs = ['test/**/test*.rb']
end
2条答案
按热度按时间wfauudbj1#
要单独运行测试,请使用
rake test:isolated
而不是rake test
。https://github.com/minitest/minitest#rake-tasks-
似乎您的其他测试之一加载了
Pathname
。j5fpnvbx2#
你的Rakefile看起来怎么样,它没有得到
pathname
要求吗?它可以解释为什么你的测试(从rake test
开始)进行得很顺利。为什么不希望在lib/util/input_file.rb的顶部使用
pathname
呢?