如何为此play_turn方法编写rspec Ruby测试?

问题描述 投票:0回答:1
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
ruby methods rspec undefined nomethoderror
1个回答
0
投票

我认为首先您必须定义实例变量@turn,以及如何对其进行递增。另外,我建议将Game#start更改为#initialize,下面的测试假定了这一点。然后,您可以检查输出到stdout的内容。

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
© www.soinside.com 2019 - 2024. All rights reserved.