#adro3030_api-automated-testing

1 messages ยท Page 1 of 1 (latest)

long elkBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1336102882155565148

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

sharp tapir
#

Here's the method being called:


def create_with_token(token, name = nil)
    owner = self
    begin
      cid = owner.stripe_customer_id
      customer =
        if cid.present?
          Stripe::Customer.retrieve(cid)
        else
          binding.pry
          Stripe::Customer.create(description: owner.to_s, email: owner.email )
        end
        
      response = customer.sources.create(card: token)
      card =
        owner.cards.create(
          stripe_card_id: response.id,
          name: name || owner.to_s,
          expiry: Date.new(response.exp_year, response.exp_month),
          brand: response.brand,
          last4: response.last4
        )
      owner.update(default_card: card, stripe_customer_id: customer.id)
    rescue Stripe::CardError, Stripe::InvalidRequestError => e
      card = Card.new(owner: owner)
      card.errors.add(:base, e.message.chop)

    end
    card
  end
lofty topaz
#

Hi ๐Ÿ‘‹

I'm not sure what those errors mean. They don't appear to be Stripe API errors (to me).

sharp tapir
#

Yeah it's an odd one, esp since it's also a 200 which is odd

#

Ok it's still failing if I do it directly in the spec


require 'rails_helper'

RSpec.describe Payments::Audits::AuditTotals, type: :service do
  let(:event) { create(:event) }
  let(:audit_totals) { described_class.new(event_id: event.id) }

  before {
    WebMock.disable_net_connect!(allow: 'api.stripe.com')
  }

  describe '#initialize' do
    it 'initializes with an event' do
      owner = event.users.first
      # card = user.create_with_token('tok_visa')
      customer = Stripe::Customer.create(description: owner.to_s, email: owner.email )
      # expect(event.users.first.cards.count).to eq(1)
    end
  end

#..... more code here 

Maybe webmock is getting in the way

lofty topaz
#

Hmmm.... so you aren't actually hitting the Stripe API, you're just mocking the responses?

sharp tapir
#

ohhh yeah looking at the logs in stripe there are no recent activity in dev mode. I am trying to disable webmock but maybe it's still intercepting

#

aha, yes i was still stubbing stripe in another helper. Works now!

lofty topaz
#

Okay, but FYI, we advise against actually hitting the Stripe APIs using automated testing.

#

You can wind up hitting your Rate limit pretty fast that way

#

We have a guide for handling Automated testing which boils down to "Mock the responses from the Stripe API"

sharp tapir
#

Ok gotcha, in all other payment-related spec I am stubbing stripe, thought it might be more accurate / future-proof in this case to let it hit the api. but I can just stub the response.

lofty topaz
#

Yeah, we recommend hitting the API once in a while, grabbing the fresh JSON object, and then just storing that such that your mock server can return that response.

sharp tapir
#

Ok sounds good, I'll do that

#

Thanks

lofty topaz
#

Happy to shed what ๐Ÿ’ก I can ๐Ÿ™‚