Toshi
Toshi

Reputation: 6340

Rspec for complicated case

I'm new to RSpec, and struggling with how to test with mock. This is basically called when webhook comes in.

class InvoiceCreated
  def call(event)
    invoice = event.data.object

    # NOTE: Skip if the invoice is closed.
    if invoice.closed == false
      stripe_customer = invoice.customer
      payment_account = PaymentCardAccount.find_by(stripe_customer_id: stripe_customer)
      card_invoice = Invoice.find_card_invoice_in_this_month_within(payment_account: payment_account)

      card_invoice.process_invoice_items(stripe_customer: stripe_customer,
                                         event_invoice_id: invoice.id)
      card_invoice.process!(:pending, id: invoice.id)
    end
  end
end

I'd love to use mock and prevent API calling for testing for two lines of code below.

 card_invoice.process_invoice_items(stripe_customer: stripe_customer,
                                     event_invoice_id: invoice.id)

  card_invoice.process!(:pending, id: invoice.id)

how can I use mock for those?

Upvotes: 0

Views: 60

Answers (1)

hahcho
hahcho

Reputation: 1409

You can use expect_any_instance_of to check that you call Invoice#process_invoice_items and Invoice#process with the correct parameters. If you do not care about how they are called you can just stub them and use allow_any_instance_of for this.

expect_any_instance_of(Invoice).to receive(:process_invoice_items)
expect_any_instance_of(Invoice).to receive(:process!)

See here for more detailed examples and got through the basic section of rspec-mock to see what you can do with it actually.

Upvotes: 1

Related Questions