RSpec hash checking due to character, not Rails

I am not using Rails, just Ruby and RSpec and am trying to pass the hash pair test. The correct result goes through the IRB, but the test keeps including the halfway point which causes the test to fail.

Here is the RSpec test:

 describe Menu do
      let(:menu) { described_class.new }
      let(:book) { double :book, name: 'Clockwise to Titan', price: 6 }

      it 'can add a dish to the menu list' do
        menu.add(book)
        expect(menu.list).to eq({'Clockwise to Titan': 6})
      end
    end

      

Here's the failure:

Failures:

1) Menu can add a dish to the menu list
   Failure/Error: expect(menu.list).to eq({'Clockwise to Titan': 6})

   expected: {:"Clockwise to Titan"=>6}
        got: {"Clockwise to Titan"=>6}

   (compared using ==)

   Diff:
   @@ -1,2 +1,2 @@
   -:"Clockwise to Titan" => 6,
   +"Clockwise to Titan" => 6,

 # ./spec/menu_spec.rb:9:in `block (2 levels) in <top (required)>'

      

I found several links on Stack Overflow for a similar problem solved by HashWithIndifferentAccess, but I am not using Rails. Also, sometimes the suggested stringify_keys method doesn't work.

+3


source to share


1 answer


From the code it looks like you should change:

expect(menu.list).to eq({'Clockwise to Titan': 6})

      

to

expect(menu.list).to eq({'Clockwise to Titan' => 6})

      

to skip the specification.

In your case, the problem: you have identified hash

where the key is not String

, but <24>.



Consider this:

{'Clockwise to Titan': 6} == {:'Clockwise to Titan' => 6}

      

but

{'Clockwise to Titan': 6} != {'Clockwise to Titan' => 6}

      

Hope this helps!

+3


source







All Articles