如何为该方法 play_turn 编写 rspec Ruby 测试?
How do I write an rspec Ruby test for this method play_turn?
class Game
def start
@player1 = Player.new("don")
@player2 = Player.new("tum")
end
def player_turn
if @turn.even?
puts "this is #{@player2.name}'s turn"
else
puts "this is #{@player1.name}'s turn"
end
end
end
我想首先你必须定义实例变量 @turn
,以及它是如何递增的。此外,我建议将 Game#start 更改为#initialize,下面的测试假设了这一点。
然后你可以检查输出到标准输出的内容。
RSpec.describe Game do
describe "#player_turn" do
context 'when the turn is even' do
let(:game) { Game.new }
it "tells you it is player 2's turn" do
expect do
game.player_turn
end.to output("this is tum's turn\n").to_stdout
end
end
end
end
class Game
def start
@player1 = Player.new("don")
@player2 = Player.new("tum")
end
def player_turn
if @turn.even?
puts "this is #{@player2.name}'s turn"
else
puts "this is #{@player1.name}'s turn"
end
end
end
我想首先你必须定义实例变量 @turn
,以及它是如何递增的。此外,我建议将 Game#start 更改为#initialize,下面的测试假设了这一点。
然后你可以检查输出到标准输出的内容。
RSpec.describe Game do
describe "#player_turn" do
context 'when the turn is even' do
let(:game) { Game.new }
it "tells you it is player 2's turn" do
expect do
game.player_turn
end.to output("this is tum's turn\n").to_stdout
end
end
end
end