3

好的,需要帮助进行测试。我想测试这个类是否接收到一个字母“O”,并且当调用“move_computer”方法时返回该人在 cli 上输入的内容。我的心理子处理器告诉我,这是一个简单的分配变量给某个东西,以在 STDIN 中保存随机的人工输入。只是现在没有得到它......有人指出我正确的方向吗?

这是我的课...

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

我的测试看起来像......

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
4

2 回答 2

2

因为move_computer 返回输入,我认为您的意思是:

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

我会像这样编写完整的规范:

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
于 2012-09-01T02:13:57.803 回答
1

这是我想出的答案...

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

[管理员注意...如果我可以一键选择所有代码文本并右缩进,那将是很酷的。(嗯……我以为这是过去的一个功能……?)]

于 2012-09-01T03:52:22.590 回答