如何使用 RSpec 测试消息的方法?基本上我的方法接受 2 个参数,如果提供了正确的详细信息,他们应该会看到一条成功消息。我试过这个:
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
我希望测试当一个有效的选择和足够的钱被传递给该方法时,是否会返回正确的消息。在这种情况下,消息还将包括传递给方法的字符串变量。我尝试了以下方法:expect @method.check_money("Coca Cola", "5.00").raise ("Purchase Complete : Coca Cola")
。也试过@method.check_money("Coca Cola", "4.59").should eq ("Purchase Complete: Coca Cola")