我有一个订单模型,其中has_many
付款和结帐控制器。如果没有付款退出,控制器应该创建一个新的付款。
private
# Helper method allows calling from several controller-callbacks.
def add_payment_if_not_exists
if @order.payments.empty?
Payment.create(...)
end
end
现在我想指定这种行为CheckoutControllerSpec
it 'should not add a payment when already added' do
@order = mock_model(Order)
@order.payments << mock_model(Payment).as_null_object
Payment.should_not_receive(:new)
post :homecoming, @params
end
但这抛出
Failure/Error: @order.payments << mock_model(Payment).as_null_object
Mock "Order_1003" received unexpected message :payments with (no args)
不知何故,我仍然没有完全理解 rspecs stubbing 和 mocking 的概念。我怎么了?