如果我没有正确的require语句,如何编写一个失败的Ruby(Minitest)单元测试?

jjjwad0x  于 2022-12-29  发布在  Ruby
关注(0)|答案(2)|浏览(117)

我有这样的测试:

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
wfauudbj

wfauudbj1#

要单独运行测试,请使用rake test:isolated而不是rake test
https://github.com/minitest/minitest#rake-tasks-
似乎您的其他测试之一加载了Pathname

j5fpnvbx

j5fpnvbx2#

你的Rakefile看起来怎么样,它没有得到pathname要求吗?它可以解释为什么你的测试(从rake test开始)进行得很顺利。
为什么不希望在lib/util/input_file.rb的顶部使用pathname呢?

相关问题