【发布时间】:2022-07-06 03:00:38
【问题描述】:
我有这个方法 - choose_option enter image description here
还有这个帮助方法 enter image description here
问题,我如何测试并获得绿灯simpelcov。
allow(adapter).to receive(:gets).and_return('my_guess')
except(adapter).to receive(:adapter_my_guess)
但这不起作用 错误:
expected: 1 time with any arguments
received: 2 times
因为我的包装器一直调用 choose_option 看起来像递归
def adapter_my_guess(guess)
@game.instance_variable_set(:@secret_code, %w[1 2 3 4])
if @game.attempts.positive?
puts show_guess_result(@game.my_guess(guess))
else
puts I18n.t(:lose_game, secret_code: @game.instance_variable_get(:@secret_code))
end
end
def wrapper(method_for_wrap)
puts method_for_wrap
@game.win == false ? choose_option : Finishe.new(@game, @statistics).finishe
end
def choose_option
option = input(I18n.t(:start_game_options, option1: OPTION_1, option2: OPTION_2, exit: EXIT))
case option
when OPTION_1 then wrapper(adapter_my_guess(input(I18n.t(:puts_guess, exit: OR_EXIT))))
when OPTION_2 then wrapper(adapter_give_hints)
else
raise(StandardError, WRONG_OPTION)
end
rescue StandardError => e
puts e
choose_option
结束
【问题讨论】:
-
图像中的代码很难处理。请将您的代码粘贴到问题中好吗?
-
你好,我会试试的!
-
我只需要在 adapter_my_guess 中测试所有案例
-
您没有测试
adapter_my_guess中的代码,因为except(adapter).to receive(:adapter_my_guess)正在模拟它。我们可以看看你的测试吗? -
抱歉,我需要在 choose_option 中测试所有情况,for_adapter 游戏我已经测试过了,抱歉。