Tic tac toe spec problem

Hi all,

Having a bit of a problem with one of the tic tac toe specs for the game class. I am failing “gets a move from the current player and performs it” test in a way I don’t really understand.

My error message is <Board … > received :place_mark with unexpected arguments.

expected: (:move, :X)
     got: ([0, 0], :X).

Note that in the game_spec file , get_move for the human player is set up to return [0, 0] in line 30:
allow(player_one).to receive(:get_move).and_return([0, 0]).

But for the test, it expects to return something called “:move”

 expect(player_one).to receive(:get_move).and_return(:move)
 expect(game.board).to receive(:place_mark).with(:move, :X)

It feels like :move is supposed to be [0,0] in this case or “a return from get_move” generally, but it’s only used in those two lines. I don’t really understand how to define / get a “:move” return

I’ll appreciate any help,

~ Paul

What does your code for Game#play_turn look like?

the test is expecting the objects to receive specific messages (i.e. have specific methods called on the objects) when play_turn is called.

note that: since player_one is an rspec double here and not an not an actual instance of the Player class .and_return is dictating what .get_move will return during the test itself

Thanks for the reply John!

My Game#play_turn looks like this:

  def play_turn
    # debugger
    @current_player == @player_one ? a = @human : a = @computer
    @board.place_mark(a.get_move, @current_player.mark)
    switch_players!
  end

Where @human and @computer are instances of my HumanPlayer and ComputerPlayer classes I set up in the initialize block (e.g. ‘@human = HumanPlayer.new(player_one.name)’)

I feel like I’m calling the right methods, per rspec requirement. I’m just returning [0,0] – as the rspec established – and not :game, as it requires.

Looking at your code, I would guess that your method would actually play the game. But the specs expect the game to be built in a specific way, which you are not doing.

I would start by refactoring your code a bit:

Each line line should do one exceedingly simple thing; this will help you debug. (e.g. don’t pass a method parameters that are themselves method calls on objects, that’s too complicated)

And then think a bit about what your Game class should care about and be responsible for:

Currently your play_turn method’s behavior is dependent on whether or not the @current_player is @player_one or @player_two and it’s also dependent on whether or not that player is a computer or a human. Try to find a way to make the method not care about who the @current_player is exactly or what class the player is an instance of. You may need to revisit your initialize method, overall the game shouldn’t care if a player is a human or computer.

hopefully this helps and it isn’t too vague, you’re close to getting it solved!

You weren’t too vague, I got it working. Thanks for the help!