How do I test a method for a message using RSpec? Basically my method accepts 2 parameters and if the right details have been supplied they should see a success message. I have tried this:
DRINKS_MACHINE = {
'Coca Cola' => 2.99,
'Fanta' => 3.27,
'Beer' => 5.99
}
class Drink
def check_money(drink_selection, money_amount_paid)
amount_paid = money_amount_paid.to_f
if amount_paid <= 0
raise ArgumentError, 'Insert Money'
end
if not DRINKS_MACHINE.has_key?(drink_selection)
raise ArgumentError, "Invalid selection: #{drink_selection}"
end
cost = DRINKS_MACHINE[drink_selection]
if amount_paid < cost
remainder = amount_paid - cost
raise ArgumentError, "Not enough coins. Insert #{remainder} more!"
elsif
puts "Purchase Complete: #{drink_selection}"
end
end
end
I wish to test that when a valid selection and enough money is passed to the method the correct message is returned. In this case the message will also include the string variable that was passed to the method. I have tried the following: expect @method.check_money("Coca Cola", "5.00").raise ("Purchase Complete : Coca Cola"). Have also tried @method.check_money("Coca Cola", "4.59").should eq ("Purchase Complete: Coca Cola")
"Purchase Complete"returned by the method or is it an exception raised?