我有这种方法用于tic tac tow游戏:
def start(token)
if token == "X"
puts "#{@player1}: Please enter a number for your X to go"
elsif token == "O"
puts "#{@player2}: Please enter a number for your O to go"
end
player_input = STDIN.gets.chomp
locate_player_input(token, player_input)
end
我只是试图测试是否正确的东西放到了终端上。我有这个测试:
describe "#start" do
context "X player's turns" do
it "prints proper messege" do
expect(STDOUT).to receive(:puts).with("Harry: Please enter a number for your X to go")
game.start("X")
end
end
end
但我有一种感觉game.start(" X")行是没有使这项工作。如何编写测试以检查puts语句是否正确输出?
答案 0 :(得分:0)
我想我明白了。由于我的函数首先放置一些东西,然后调用下一个方法来运行,我需要另一个期望语句。我的通过测试是这样的:
describe "#start" do
context "X player's turns" do
it "prints proper messege" do
expect(STDOUT).to receive(:puts).with("Harry: Please enter a number for your X to go")
expect(game).to receive(:get_input)
game.start("X")
end
end
end
我不确定这是否正确,但测试确实通过了。