1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Uri, v good point, ammend made, however its still having issues with the syntax i've used? error as below: gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load': /Users/jamesbkemp/makers/week4/takeaway/spec/restaurant_spec.rb:84: syntax error, unexpected '}', expecting => (SyntaxError) ...mer: customer1, name: 'tea', 2},{customer: customer1, name: ... ...
@user3346954 ...'tea', 2} should be ...'tea', volume: 2} - updated my answer.
ha ha ha, Uri, you're a star, thanks for pointing out what 30 minutes of staring at the same page i couldn't find!!!! Really appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.