0

Ok so I am an rspec noob and I am trying to test a method that sits inside of a module. Below is my test file, the code I am testing against, and the error I am getting. Any guidance would be appreciated.

Thanks in advance.

Here is my spec file gamestat_spec.rb

require 'gamestate'
require 'board'


describe 'Gamestate module' do
  before (:each) do
    @player_human = Player.get_player('X')
    @player_computer = Player.get_player('O')    
  end
  describe 'is_a_human_win' do
    it 'receives current board' do
      class GamestateTester
        include ::GameState
      end
      human_win_test = GamestateTester.new
      myboard = Board.new
      myboard.grid[:a1] = "X"
      myboard.grid[:b2] = "X"
      myboard.grid[:c3] = "X"
      human_win_tester.is_a_human_win(@myboard).should_receive(:puts)
    end
    xit 'looks for human win'
    xit 'if win found returns...Human Wins'
  end
  describe 'is_a_computer_win' do
    xit 'receives current board'
    xit 'looks for computer win'
    xit 'if win found returns...Computer Wins'
  end
end

and here is the module file I am testing against...gamestate.rb....

module GameState

  def is_a_human_win(board)

  win_moves = {
    :wm01 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"X"},
    :wm02 => {:a1=>" ", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>" ", :c2=>"X", :c3=>" "},
    :wm03 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>"X", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "},
    :wm04 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>"X", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "},
    :wm05 => {:a1=>"X", :a2=>"X", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
    :wm06 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>"X", :c3=>"X"},
    :wm07 => {:a1=>"X", :a2=>" ", :a3=>" ", :b1=>"X", :b2=>" ", :b3=>" ", :c1=>"X", :c2=>" ", :c3=>" "},
    :wm08 => {:a1=>" ", :a2=>" ", :a3=>"X", :b1=>" ", :b2=>" ", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>"X"}
  }

  x_on_the_gameboard = board.grid.select{ |k, v| v == "X" }.keys


    win_moves.each do |k,v|
      win_moves_keys = v.select{ |k, v| v == "X"}.keys

      matching_moves = win_moves_keys & x_on_the_gameboard

      if matching_moves.length >= 3 

        p 'key: '+k.to_s

        string_contains = k.to_s
        if string_contains =~ /wm/ 
          puts "Human Wins"
          # return 1
          exit
        else

        end
      end
    end
  end

  def is_a_computer_win(board)

  ai_winmoves = {
    :wm01 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>"O"},
    :wm02 => {:a1=>" ", :a2=>"O", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>"O", :c3=>" "},
    :wm03 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "},
    :wm04 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>"O", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>" "},
    :wm05 => {:a1=>"O", :a2=>"O", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "},
    :wm06 => {:a1=>" ", :a2=>" ", :a3=>" ", :b1=>" ", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>"O", :c3=>"O"},
    :wm07 => {:a1=>"O", :a2=>" ", :a3=>" ", :b1=>"O", :b2=>" ", :b3=>" ", :c1=>"O", :c2=>" ", :c3=>" "},
    :wm08 => {:a1=>" ", :a2=>" ", :a3=>"O", :b1=>" ", :b2=>" ", :b3=>"O", :c1=>" ", :c2=>" ", :c3=>"O"}
  }

  o_on_the_gameboard = board.grid.select{ |k, v| v == "O" }.keys


    ai_winmoves.each do |k,v|
      ai_winmoves_keys = v.select{ |k, v| v == "O"}.keys

      matching_moves = ai_winmoves_keys & o_on_the_gameboard

      if matching_moves.length >= 3 

        p 'key: '+k.to_s

        test_string_contains = k.to_s
        if test_string_contains =~ /wm/ 
          puts "Computer Wins"
          # return -1
          exit
        else

        end
      end
    end
  end
end

and here is the error I am getting....

Failures:

  1) Gamestate module is_a_human_win receives current board
     Failure/Error: human_win_tester.is_a_human_win(@myboard).should_receive(:puts)
     NameError:
       undefined local variable or method `human_win_tester' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x007f8ef43fa0b8>
     # ./spec/gamestate_spec.rb:20:in `block (3 levels) in <top (required)>'

Finished in 0.01865 seconds
23 examples, 1 failure, 6 pending
thefonso
  • 3,220
  • 6
  • 37
  • 54

2 Answers2

1

Looks like it's not even an Rspec problem, just ruby syntax. :)

This part:

 class GamestateTester
   include 'gamestate'
 end

Should be:

 class GamestateTester
   include ::GameState
 end
pje
  • 21,801
  • 10
  • 54
  • 70
  • now I get this....uninitialized constant GamestateTester::Gamestate....(apologies for my noobness) @pje – thefonso Nov 27 '12 at 18:10
  • you also have to require the gamestate file in your test or test helper... so at the top of that test file: `require 'gamestate'` then include the module in your GamestateTester class as shown above. – Dave Giunta Nov 27 '12 at 18:15
  • @thefonso: I've edited the answer. Check out [this answer](http://stackoverflow.com/a/5318496/1004889) for a better explanation. – pje Nov 27 '12 at 18:16
  • @DaveGiunta ok..I've updated the test file but now getting...uninitialized constant Gamestate...error. see above for new code listing and error. – thefonso Nov 27 '12 at 18:31
  • ah...hang on... it was the name of the module 'GameState' I had 'Gamestate"....ugh evil camel case. – thefonso Nov 27 '12 at 18:37
  • Ok this works...(thanks Dave and pje)...I also had to do some other changes...for anyone whom this will help in future, I've put the updated code onto this gist...https://gist.github.com/4156537 – thefonso Nov 27 '12 at 19:49
0

Simple old typo. You have an extra er in your object ;)

human_win_test = GamestateTester.new
...
human_win_tester.should ....
CubaLibre
  • 1,675
  • 13
  • 22