如何测试此方法RSPEC
我有这个方法 - select_option 态
问题,我如何测试和参加绿灯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
因为我的包装器一直都在调用select_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
结束
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
evef(apapter).to接收(:adapter_my_guess)
说ADAPTER
将接收一个和只有一个呼叫adapter_my_guess
。它收到了两个。如果是正确的,请 at_least(1).Time ,期待更多电话。
如果这是不正确的,我们需要更多地了解您的代码和测试。
一些注释...
不使用
instance_variable_get
和instance_variable_set
在生产代码中。抓住另一个对象的内部变量将代码纠缠不清,并使对象很难而不会产生不可预见的后果。 makesecret_code
适当的 consector方法。select_option
做三件事。它获取选项,它可以解释选项,并且决定是否需要再次获得选项。将其分为三种方法,一种用于获取选项,一种解释选项,一种将它们放在一起。这将更加灵活,更易于测试。抓住每个标准词都太宽了。通过方法拆分,该选项是对方法的参数。您可以将异常更改为更具体的 grognn> grigenterror 。
现在,每个都可以进行单位测试,而无需模拟整个过程。例如,我们需要在
select_option
中测试的只是它是否有一个选项,尝试使用它并进行重试。我们不需要知道什么
do_option
norget_option
可以测试select_option
,可以完全模拟它们。do_option
和get_option
可以单独测试。那是基本方法。
except(adapter).to receive(:adapter_my_guess)
saysadapter
will receive one and only one call toadapter_my_guess
. It's received two.If that is correct, change your expectation to expect more calls by adding
at_least(1).time
.If that is not correct, we'd need to know more about your code and test.
Some notes...
Do not use
instance_variable_get
andinstance_variable_set
in production code. Grabbing at other object's internal variables tangles up the code and makes it hard to change the object without unforeseen consequences. Makesecret_code
a proper accessor method.choose_option
does three things. It gets the option, it interprets the option, and it decides if it needs to get the option again. Split it into three methods, one to get the option, one to interpret the option, and one to put them together. That will be more flexible and easier to test.Catching every StandardError is too broad. With the methods split, the option is an argument to a method. You can change the exception to be a more specific ArgumentError.
Now each can be unit tested without having to mock the whole process. For example, all we need to test in
choose_option
is whether it gets an option, tries to do something with it, and retries.We don't need to know what
do_option
norget_option
does to testchoose_option
, they can be totally mocked.do_option
andget_option
can be unit tested separately.That's the basic approach.