Search code examples
rubycapybaraminitestassertions

How to make minitest available in ruby module


I am trying to make a module that has a bunch of helper definitions. Some of the defs use assert from Minitest::Assertions. How can i get access to these assertions inside a module. Mock example below

module Helper
  require 'minitest'

  TEST = Minitest::Assertions

  module Helper::Scripts
    def self.assertion
       TEST.assert true 
    end
  end
end

Helper::Scripts.assertion #undefined assert

Solution

  • You need to define an instance accessor named assertions

    module TestHelpers
      include Minitest::Assertions
    
      # Number of assertions executed in this run
      attr_accessor :assertions
    
      def assert_true
        self.assertions = 0
    
        assert true
      end
    end
    

    We can use TestHelpers this way:

    class FooTest
      include TestHelpers
    end
    
    test = FooTest.new
    test.assert_true #=> true