2015-02-05 70 views
0

我學習Ruby和RSpec說話,我已經遇到了障礙,其中大部分提供學習材料已成爲過時,我缺少的詞彙通過殘骸進行篩選。測試文件和測試文件通過相互

class Session 

    def initialize(winning_score = 0) 
    @winning_score = winning_score 
    play 
    end 

    def play 
    get_players 
    max_score 
    while #game is in play 
     print_score 
     #play game 
    end 
    winner 
    end 

    def get_players 
    puts "\nPlayer X name:" 
    p1 = gets.chomp.upcase 
    @player1 = Player.new(p1, "X", 0) 
    puts "\nPlayer O name:" 
    p2 = gets.chomp.upcase 
    @player2 = Player.new(p2, "O", 0) 
    end 

    def max_score 
    puts "\nBest out of how many?" 
    max = gets.chomp 
    @winning_score = (max.to_f/2).ceil 
    end 

    def print_score 
    puts "\n#{@player1.name}: #{@player1.score} \n#{@player2.name}: #{@player2.score}" 
    end 

    def winner 
    if @player1.score == @winning_score 
     puts "\n#{@player1.name} WINS!!!" 
    elsif @player2.score == @winning_score 
     puts "\n#{@player2.name} WINS!!!" 
    end 
    end 
end 

class Player 

    attr_accessor :name, :mark, :score 

    def initialize(name, mark, score) 
    @name = name 
    @mark = mark 
    @score = score 
    end 

end 

RPEC:

describe "Play" do 

    before(:each) do 
    allow(x).to receive(:puts) 
    allow(x).to receive(:print) 
    end 

    let(:x) { Session.new } 

    it "displays game score" do 
    @player1 = Player.new("p1", "X", 0) 
    @player2 = Player.new("p2", "O", 2) 
    expect(x).to receive(:puts).with("\np1: 0 \np2: 2") 
    x.print_score 
    x.play 
    end 
end 

...我認爲這是所有的代碼適用的位......問題是,該文件被測試與RSpec的文件保留通過相互交談,我不斷收到這樣的事情:

1) play displays game score 
Failure/Error: expect(x).to receive(:puts).with("\np1: 0 \np2: 2") 
    #<Session:0x007fc16b9f5d38> received :puts with unexpected arguments 
    expected: ("\np1: 0 \np2: 2") 
      got: ("\n\t\t: 0 \n\tEND: 0"), ("\nPlayer X name:"), ("\nPlayer O name:"), ("\nBest out of how many?"), ("\n\tIT \"GETS AND CREATES PLAYERS\" DO WINS!!!") 
# ./tictactoe_spec.rb:36:in `block (2 levels) in <top (required)>' 

...其中噪聲是其他方法gets.chomping運行RSpec的代碼,並把它作爲玩家的名字......我無法弄清楚如何預防這從發生清除/重置它,甚至是正確的行動方案......請告知。

回答

0

的解決方案是實現在初始化一個可選的輸出源默認爲標準輸出,然後創建輸出源的兩倍方法來放至。在與幾位有經驗的開發者交談之後,這似乎是一件相當普遍的事情。簡化代碼可能也會非常有幫助...回頭看這很糟糕。

0

那麼,你是設置在測試方面的@player1@player2實例變量。而你在會話對象中需要它們。

我覺得這裏的好辦法是存根get_players方法,但是我們將不得不改變它一下。

class Session 

    #... 

    def get_players 
    @player1 = get_player("X") 
    @player2 = get_player("O") 
    end 

    def get_player(mark) 
    puts "\nPlayer #{mark} name:" 
    name = gets.chomp.upcase 
    Player.new(name, mark, 0) 
    end 

    #... 

end 

現在你可以存根那些get_player調用

# ... 

    it "displays game score" do 
    allow(x).to recive(:get_player).with("X") { Player.new("p1", "X", 0) } 
    allow(x).to recive(:get_player).with("O") { Player.new("p2", "O", 2) } 

    expect(x).to receive(:puts).with("\np1: 0 \np2: 2") 
    x.print_score 
    x.play 
    end 
+0

我實施了這些建議(有兩個錯別字:「recive」應該是「receive」),但它仍然是交叉談話。 你能否闡述一下存根概念了一下,究竟是怎麼回事呢? 另外,爲什麼它需要x.print_score和x.play其餘的測試代碼?我見過這個,但不明白。關於這個問題,我很少能找到。 – spectre6000 2015-02-06 16:48:53

+0

1)play顯示遊戲得分 失敗/錯誤:expect(x).to接收(:puts).with(「\ np1:0 \ np2:2」) # received:puts with unexpected arguments (「\ np1:0 \ np2:2」) got:(「\ n:0 \ n \ tIT \」獲取並創建播放器\「DO:0」),(「\ n從多少?「),(」\ np1 WINS !!!「) #./tictactoe_spec.rb:32:in ' – spectre6000 2015-02-06 16:51:05