我正在尝试实施和学习测试(看起来像minitest是要走的路)。而且我很难测试内部模块类方法。
这或多或少是我试图做的用例。 (也许我会以完全错误的方式去做)
module Zombie
class << self
# This is the method/code I want to test/execute
def intimidate
roar('slardar')
end
# This is the method that is internal, that I want to stub.
# Actual method(not this mocked one) is stateful. So I want to have
# mocked predefined data.
def roar(a)
'rawrger' + a
end
end
end
# Test Thingy
class ZombieTest < Minitest::Test
def test_mr_mock
@mock = Minitest::Mock.new
@mock.expect(:roar, 'rawrgerslardar', ['slardar'])
Zombie.stub :roar, @mock do
Zombie.intimidate
end
@mock.verify
end
end
答案 0 :(得分:2)
您可以使用lambda传递参数:
class ZombieTest < Minitest::Test
def test_mr_mock
@mock = Minitest::Mock.new
@mock.expect(:roar, 'rawrgerslardar', ['slardar'])
Zombie.stub :roar, ->(a) { @mock.roar(a) } do
Zombie.intimidate
end
@mock.verify
end
end