如何使 minitest 在 ruby 模块中可用
How to make minitest available in ruby module
我正在尝试制作一个包含大量辅助程序定义的模块。一些 defs 使用来自 Minitest::Assertions 的断言。我怎样才能访问模块内的这些断言。下面的模拟示例
module Helper
require 'minitest'
TEST = Minitest::Assertions
module Helper::Scripts
def self.assertion
TEST.assert true
end
end
end
Helper::Scripts.assertion #undefined assert
您需要定义一个名为 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
我们可以这样使用 TestHelpers:
class FooTest
include TestHelpers
end
test = FooTest.new
test.assert_true #=> true
我正在尝试制作一个包含大量辅助程序定义的模块。一些 defs 使用来自 Minitest::Assertions 的断言。我怎样才能访问模块内的这些断言。下面的模拟示例
module Helper
require 'minitest'
TEST = Minitest::Assertions
module Helper::Scripts
def self.assertion
TEST.assert true
end
end
end
Helper::Scripts.assertion #undefined assert
您需要定义一个名为 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
我们可以这样使用 TestHelpers:
class FooTest
include TestHelpers
end
test = FooTest.new
test.assert_true #=> true