如何解决在rspec中硬编码我的值不起作用
我的下课是这样的:
attr_reader :player,:player_choice,:cpu_choice,:choices,:game,:result
def initialize(player)
@player = player
@player_choice = ""
@cpu_choice = ""
@choices = Choices.new
@result = ""
end
def get_result
@result
end
def show_cpu_choice
@cpu_choice
end
def set_player_choice(choice)
@player_choice = choice
set_cpu_choice
decide_winner
end
def set_cpu_choice
@cpu_choice = @choices.get_choices.sample
end
我已经省略了任何不相关的方法,但是基本上我想将@cpu_choice硬编码为“ Scissors”,因此以下测试可以正常进行,因为我的cpu选择是随机生成的,但是无论我在rspec中尝试哪种方法都无法工作。 我的set_cpu_choice随机选择来自我的Choices类btw中的实例变量的数组。
let(:game) {Game.new("Johnny")}
describe 'Player wins' do
it 'Player selects Rock and CPU has picked Scissors' do
game.set_player_choice("Rock")
allow(game).to receive(:show_cpu_choice).and_return("Scissors")
expect(game.get_result).to eq("Johnny")
end
end
我已经在我的rspec中尝试了上面的方法,也尝试使用instance_variable_set来做到这一点,但是我的测试仍然在随机选择CPU选择。
我也看过一种叫“ srand”的东西,但是对我来说这看起来太复杂了,因为我什么都不懂。
解决方法
因此,我假设decide_winner
调用show_cpu_choice
并设置@result
。问题是尚未调用set_player_choice game
时返回“ Scissors”。 allow
应该移到对set_player_choice
的调用之前
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。