Expect to create a new object

I need to write that a new object will be created in the payment system. Code included from order.rband order_spec.rb( Orderclass 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

I want to understand how I can write an appropriate specification to verify that a new Payment object has been created. I found this article from Semaphore CI useful, but I'm not sure about the solution. I am pretty sure that I need to create a double test version, and then maybe the stub to method allow(order).to receive(:confirm_order).and_return(#new_object??).

+4
source share
1 answer

Order, Order. Payment, , confirm_order.

let(:payment) { double :payment }

it "returns a payment" do
  expect(Payment).to receive(:new).with(:credit_card).and_return(payment)

  order.add_product(product, 3)
  expect(order.confirm_order(:credit_card)).to eq(payment)
end

, Order, Order, , Order .

+8

Source: https://habr.com/ru/post/1626827/


All Articles