gpt4 book ai didi

ruby - stub 和模拟 Minitest

转载 作者:行者123 更新时间:2023-12-05 04:12:44 24 4
gpt4 key购买 nike

我正在尝试实现和学习测试(似乎 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

最佳答案

您可以使用 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

关于ruby - stub 和模拟 Minitest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39889537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com