paulo.techie
paulo.techie

Reputation: 3

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

Upvotes: 0

Views: 39

Answers (1)

Tim Urian
Tim Urian

Reputation: 36

I think first you are going to have to define the instance variable @turn, and how it is incremented. Additionally I would recommend changing the Game#start to #initialize, the test below assumes this. Then you can check what is output to 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

Upvotes: 2

Related Questions