Michael Durrant
Michael Durrant

Reputation: 96484

ruby - rspec - how to compare to a hash? Syntax error, unexpected =>

My hash seems to be ok, why am I getting the syntax errors ?

Getting

SyntaxError:
  .../x_and_o_spec.rb:14: syntax error, unexpected =>, expecting '}'
      expect(board).to eq {0 =>"-", 1 =>"-",  2 =>"-",  3 =>"
                               ^
  .../x_and_o_spec.rb:14: syntax error, unexpected ',', expecting keyword_end
   expect(board).to eq {0 =>"-", 1 =>"-",  2 =>"-",  3 =>"-", 
                                ^
  .../x_and_o_spec.rb:14: syntax error, unexpected ',', expecting end-of-input
  oard).to eq {0 =>"-", 1 =>"-",  2 =>"-",  3 =>"-",  4 =>"-",

If i comment out my expect and just print the hash I get:

{0=>"-", 1=>"-", 2=>"-", 3=>"-", 4=>"-", 5=>"-", 6=>"-", 7=>"-", 8=>"-", 9=>"-"}

so why does my expect give those error messages ?

Code

class Grid
  attr_accessor :board
  def initialize
    @board = {}
    (0..9).each do |key|
      @board[key] = "-" 
    end 
  end 
end

Test

it 'Grid has 9 elements, each element is a value of nil, o or X' do
  board = Grid.new.board
  expect(board).to eq {0 =>"-", 1 =>"-",  2 =>"-",  3 =>"-",  4 =>"-",  5 =>"-",  6 =>"-",  7 =>"-",  8 =>"-",  9 =>"-"}
end 

Upvotes: 1

Views: 239

Answers (1)

Michael Durrant
Michael Durrant

Reputation: 96484

Programmer ignorance (me) around parser rules if the expect is a hash.
and not realizing the hash / block issue with using {}'s

Putting parens around it solved it!

 expect(board).to eq    ({0 =>"-", 1 =>"-",  ...  9 =>"-"})
 # added these parens: /\                                /\

Upvotes: 4

Related Questions