2012-08-31 155 views
3

好的,需要幫助制定測試。我想測試一下,這個班級收到一個字母「O」和 ,當被稱爲「move_computer」的方法返回時,人員進入cli。我的智力子處理器告訴我這是一個簡單的賦值變量來保存STDIN中的隨機輸入。現在只是沒有得到它......任何人都指向了正確的方向?如何用RSpec測試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 

回答

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 
+0

謝謝你親切先生。 – thefonso

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 

[注到管理員...這將是冷靜,如果我可以選擇我的所有代碼的文本和右縮進在一鍵按下。 (嗯......我認爲這是過去的一個功能......?)]

+1

有一個按鈕看起來像「{}」,我認爲它符合您的描述。 –