【发布时间】:2016-05-09 14:50:06
【问题描述】:
我需要编写一个期望,即将在支付系统中创建一个新对象。 order.rb 和 order_spec.rb 中包含的代码(Order 类为 here):
#order.rb
def confirm_order(method_of_payment)
if credit_card_but_products_out_stock?(method_of_payment)
raise "Cannot make credit card payment now, as some products are out of stock"
end
order_total
Payment.new(method_of_payment, self.total)
end
#order_spec.rb
it 'it creates a new payment object if method_of_payment is valid and order.total is > 0' do
order.add_product(product, 3)
order.confirm_order(:credit_card)
#Expect that a new payment object is created.
end
我想了解如何编写适当的规范来测试是否创建了新的 Payment 对象。我发现来自 Semaphore CI 的 this article 很有用,但不确定解决方案。我很确定我应该创建某种测试替身,然后可能是 allow(order).to receive(:confirm_order).and_return(#new_object??) 的方法存根。
【问题讨论】: