jbk
jbk

Reputation: 2202

Rspec testing contents of hash within an array

i'm trying to test the contents of an instance variable for the presence of two hashes and their contents, my code is as below. 1) the rspec error message is get is due to my 'containers' in the test; ([{}]), whats wrong with what i'm doing here? 2) Am i going about rspec testing the value assigned to an instance variable in the right way in the first place? Thanks in advance.

ruby code below

def order_received(customer, name, volume)
  if @menu.any? { |h| h[:name] == name}
    @orders << {customer: customer, name: name, volume: volume}
    else
    customer.not_on_menu(name)
  end
end


def place_order(restaurant, name, volume)
  restaurant.order_received(self, name, volume)
end

rspec tests below

it 'confirm order by giving the list of dishes, their quantities, and the total price' do 

  restaurant1 = Restaurant.new
  customer1 = Customer.new
  restaurant1.create_dish('coffee', 2)
  restaurant1.create_dish('tea', 2)
  customer1.place_order(restaurant1, 'tea', 2)
  customer1.place_order(restaurant1, 'coffee', 3)

  expect(@orders).to eq([{customer: customer1, name: 'tea', 2},{customer: customer1, name: 'coffee', 3}])

end

Upvotes: 1

Views: 502

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

I believe you are trying to check the value of @orders inside the restaurant1 object. Rspec does not know what @orders is, and has no way of guessing. You should check it by testing the actual value (I'm assuming restaurant1.order is available).

Also, you forgot to give a key to your last element in the map (volume), which caused your syntax error:

it 'confirm order by giving the list of dishes, their quantities, and the total price' do 

  restaurant1 = Restaurant.new
  customer1 = Customer.new
  restaurant1.create_dish('coffee', 2)
  restaurant1.create_dish('tea', 2)
  customer1.place_order(restaurant1, 'tea', 2)
  customer1.place_order(restaurant1, 'coffee', 3)

  expect(restaurant1.orders).to eq([{customer: customer1, name: 'tea', volume: 2},{customer: customer1, name: 'coffee', volume: 3}])

end

Upvotes: 1

Related Questions