Home > Software engineering >  How to write a Ruby (Minitest) unit test that fails if I don't have the correct require stateme
How to write a Ruby (Minitest) unit test that fails if I don't have the correct require stateme

Time:12-27

I have this test:

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

for this code:

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('%sd.txt', basename, @num))                                                                                                                
    end                                                                                                                                                                           
  end                                                                                                                                                                             
end

When I run this with rake test, everything passes as expected; however, when I call it from my actual main script, it chokes with uninitialized constant Util::InputFile::Pathname (NameError). When I add require 'pathname' at the top of lib/util/input_file.rb, everything is fine.

Why does the unit test not fail in the same way, and how can I refactor it such that it will fail unless I have the correct require statement in the production code?

EDIT: Rakefile is as follows:

require 'minitest/test_task'

Minitest::TestTask.create do |t|
  t.test_globs = ['test/**/test*.rb']
end

CodePudding user response:

How does your Rakefile look and doesn't it get pathname required? It can explain why your test (started with rake test) goes well.

And why don't you want to require pathname at the top of lib/util/input_file.rb?

CodePudding user response:

To run tests separately use rake test:isolated instead of rake test.

https://github.com/minitest/minitest#rake-tasks-

It seems like one of your other tests gets Pathname loaded.

  • Related