0
votes

I have this method for a tic tac tow game:

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

I'm only trying to test to see if the correct thing is puts'd to the terminal. I have this test:

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

But I have a feeling the game.start("X") line is what is not making this work. How can I write the test to just check if the puts statement is correctly outputted?

1
A refactoring suggestion: instead of the if, else, just use puts "#{@player2}: Please enter a number for your #{token} to go".lwassink
good looks. Thanks for the suggestion, I'll be sure to implement it when I start to refactorHarry B.
Is the test failing? What's the failure message?Johnson
The terminal is just waiting for the user to input something, which I'm assuming is caused by the gets statement in the actual method once game.start("X") is calledHarry B.

1 Answers

0
votes

I think I figured it out. Since my function was first puts-ing something and then calling the next method to be run, I needed another expect statement. My passing test is as such:

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

I'm not sure if this is correct, but the test did pass.