Include module in all MiniTest tests like in RSpec

前端 未结 3 2042
滥情空心
滥情空心 2021-02-14 14:08

In RSpec I could create helper modules in /spec/support/...

module MyHelpers
  def help1
    puts \"hi\"
  end
end

and include it

3条回答
  •  醉酒成梦
    2021-02-14 14:59

    minitest does not provide a way to include or extend a module into every test class in the same way RSpec does.

    Your best bet is going to be to re-open the test case class (differs, depending on the minitest version you're using) and include whatever modules you want there. You probably want to do this in either your test_helper or in a dedicated file that lets everyone else know you're monkey-patching minitest. Here are some examples:

    For minitest ~> 4 (what you get with the Ruby Standard Library)

    module MiniTest
      class Unit
        class TestCase
          include MyHelpers
        end
      end
    end
    

    For minitest 5+

    module Minitest
      class Test
        include MyHelperz
      end
    end
    

    You can then use the included methods in your test:

    class MyTest < Minitest::Test # or MiniTest::Unit::TestCase
      def test_something
        help1
        # ...snip...
      end
    end
    

    Hope this answers your question!

提交回复
热议问题