3

Ok, need help working out a test. I want to test that this class receives a letter "O" and that when called the "move_computer" method returns WHATEVER the person enters on the cli. my mental subprocessor tells me this is a simple assign a variable to something to hold the random human input at STDIN. Just not getting it right now...anyone point me in the right direction?

here is my class...

class Player
  def move_computer(leter)
    puts "computer move"
    @move = gets.chomp
    return @move
  end
end

my test look like...

describe "tic tac toe game" do
  context "the player class" do
    it "must have a computer player O" do

      player = Player.new()
      player.stub!(:gets) {"\n"} #FIXME - what should this be?
      STDOUT.should_receive(:puts).with("computer move")
      STDOUT.should_receive(:puts).with("\n") #FIXME - what should this be?
      player.move_computer("O")
    end
  end
end
thefonso
  • 3,220
  • 6
  • 37
  • 54

2 Answers2

2

Because move_computer returns the input, I think you meant to say:

player.move_computer("O").should == "\n"

I would write the full spec like this:

describe Player do
  describe "#move_computer" do
    it "returns a line from stdin" do
      subject.stub!(:gets) {"penguin banana limousine"}
      STDOUT.should_receive(:puts).with("computer move")
      subject.move_computer("O").should == "penguin banana limousine"
    end
  end
end
Jared Beck
  • 16,796
  • 9
  • 72
  • 97
1

Here is the answer I came up with...

require_relative '../spec_helper'

# the universe is vast and infinite...it contains a game.... but no players
describe "tic tac toe game" do
  context "the player class" do
    it "must have a human player X"do
      player = Player.new()
      STDOUT.should_receive(:puts).with("human move")
      player.stub(:gets).and_return("")
      player.move_human("X")
    end
    it "must have a computer player O" do
      player = Player.new()
      STDOUT.should_receive(:puts).with("computer move")
      player.stub(:gets).and_return("")
      player.move_computer("O")
    end
  end
end

[NOTE TO THE ADMINS...it would be cool if I could just select all my code text and right indent in one button push. (hmmm...I thought that was a feature in the past...?)]

Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
thefonso
  • 3,220
  • 6
  • 37
  • 54