How to merge two test into one RSpec

Posted by thefonso on Stack Overflow See other posts from Stack Overflow or by thefonso
Published on 2012-09-04T03:37:00Z Indexed on 2012/09/04 3:37 UTC
Read the original article Hit count: 156

Filed under:
|

Both the last two test work individually...but when both are set to run (non pending) I get problems.

question: can I create a test that merges the two into one? How would this look?(yes, I am new to rspec)

require_relative '../spec_helper'

# the universe is vast and infinite....and...it is empty
describe "tic tac toe game" do
  context "the game class" do

    before (:each) do
      player_h = Player.new("X")
      player_c = Player.new("O")
      @game = Game.new(player_h, player_c)
    end

   it "method drawgrid must return a 3x3 game grid" do
      @game.drawgrid.should eq("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n  1 2 3 \n")
      @game.drawgrid
   end
   #FIXME - last two test here - how to merge into one?
   it "play method must display 3x3 game grid" do
      STDOUT.should_receive(:puts).and_return("\na #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n----------\nb #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n----------\nc #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n----------\n  1 2 3 \n").with("computer move")
      @game.play
    end
    it "play method must display 3x3 game grid" do
      STDOUT.should_receive(:puts).with("computer move")
      @game.play
    end
  end
end

just for info here is the code containing the play method

require_relative "player"
#
#Just a Tic Tac Toe game class
class Game
  #create players
  def initialize(player_h, player_c)
    #bring into existence the board and the players
    @player_h = player_h
    @player_c = player_c
    #value hash for the grid lives here
    $thegrid = {
        :a1=>" ", :a2=>" ", :a3=>" ",
        :b1=>" ", :b2=>" ", :b3=>" ",
        :c1=>" ", :c2=>" ", :c3=>" "
    }
    #make a global var for drawgrid which is used by external player class
    $gamegrid = drawgrid

  end
  #display grid on console
  def drawgrid

    board = "\n"
    board << "a #{$thegrid[:a1]}|#{$thegrid[:a2]}|#{$thegrid[:a3]} \n"
    board << "----------\n"
    board << "b #{$thegrid[:b1]}|#{$thegrid[:b2]}|#{$thegrid[:b3]} \n"
    board << "----------\n"
    board << "c #{$thegrid[:c1]}|#{$thegrid[:c2]}|#{$thegrid[:c3]} \n"
    board << "----------\n"
    board << "  1 2 3 \n"
    return board

  end
  #start the game
  def play
    #draw the board
    puts drawgrid
    #external call to player class
    @player = @player_c.move_computer("O")
  end
end

player_h = Player.new("X")
player_c = Player.new("O")


game = Game.new(player_h, player_c)
game.play

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rspec