2012-09-22 75 views
0

我有一個類。作爲這樣的方法....這是什麼RSpec錯誤告訴我?

class Player 

    attr_reader :boardpiece # i exist so game can read me 

    def initialize(letter) 
    @boardpiece = letter 
    end 

    def move_human(game, board) 
    @game_two = game 

    puts "human move..." 

    human_move = gets.chomp 

    human_symbol = human_move.to_sym 

    # look for move as key in board.grid 
    if board.grid.has_key?(human_symbol) 
     if board.grid[human_symbol] == " " 
     #puts "bingo" 
     @move = human_symbol    
     else 
     puts "spot taken...try again" 
     move_human(@game_two, board) 
     end 
    else 
     puts "invalid move...try again" 
     move_human(@game_two, board) 
    end 

    end 
end 

我想在RSpec中寫一個測試吧...

require 'game' 
require 'board' 

describe Player do 
    describe 'move_human' do 
    it 'receives cli input' do 
     player_h = Player.new('X') 
     player_c = Player.new('O') 
     board = Board.new 
     game = Game.new(player_h, player_c, board)   

     player_h.move_human('X', board).should_receive(:puts).with('human move...') 

     game.play 
    end 
    xit 'and returns a move value' 

    end 
end 

我得到這個錯誤作爲輸出....我做錯了什麼?

[email protected] ~/Documents/ca_ruby/rubytactoe (development)$ rspec spec 

Board 
    creates a blank board with nine spaces 
    can set the value of a specified cell 
    checks if a space is taken or not 
    drawgrid 
    draws a blank grid given no input 

Player 
    move_human 
human move... 
    receives cli input (FAILED - 1) 
    and returns a move value (PENDING: Temporarily disabled with xit) 

Pending: 
    Player move_human and returns a move value 
    # Temporarily disabled with xit 
    # ./spec/player_spec.rb:16 

Failures: 

    1) Player move_human receives cli input 
    Failure/Error: player_h.move_human('X', board).should_receive(:puts).with('human move...') 
    Errno::EISDIR: 
     Is a directory - spec 
    # ./lib/player.rb:21:in `gets' 
    # ./lib/player.rb:21:in `gets' 
    # ./lib/player.rb:21:in `move_human' 
    # ./spec/player_spec.rb:12:in `block (3 levels) in <top (required)>' 

Finished in 0.00977 seconds 
6 examples, 1 failure, 1 pending 

Failed examples: 

rspec ./spec/player_spec.rb:6 # Player move_human receives cli input 
[email protected] ~/Documents/ca_ruby/rubytactoe (development)$ 

回答

2

@thefonso,不完全。您想要存儲播放器對象上發生的「gets」調用,而不是move_human方法調用的結果。

所以,你會說player_h.stub(:gets).and_return(「來自用戶的一些輸入」)。同樣的事情也適用於你的should_receive(:puts)位。在方法調用發生在調用這些方法的地方之前,您需要設置這些期望。因此,您的規格可能具有以下內容:

# These setup expectations for when "gets" and "puts" are called later... 
player_h.stub(:gets).and_return("some input from the user") 
player_h.should_receive(:puts).with("human move...") 

# And then we run the method where those expectations above will get triggered: 
player_h.move_human('X', board) 

希望有所幫助。

+0

謝謝絕地大師Giunta。 :) – thefonso

1

您可以爲您的課程存根方法。

player_h.stub(:gets).and_return("Some string") 
+0

所以像這樣.... player_h.move_human('X',board).stub(:gets).and_return(「human move ...」) – thefonso

相關問題