AdamT
AdamT

Reputation: 6485

Integration tests create test users on Stripe, how to stop or stub out

I'm using stripe on a project. I'm using Railscasts #288 (http://railscasts.com/episodes/288-billing-with-stripe) as a guide. I have it so that once a user registers with a valid username and password I will create their Stripe customer account.

After a few runs of my integration test I can see that I have many users created in my test account for Stripe. How do I structure the integration test so that it goes through my registration process as a typical user would, but without creating the Stripe account with Stripe?

Upvotes: 3

Views: 2614

Answers (4)

ian root
ian root

Reputation: 349

i dealt with that by making all my customers created through the testing have "deletable" in their email address, and adding these lines to my spec_helper.

config.after(:suite) do
  custs = Stripe::Customer.all(limit: 100).data
  custs.keep_if { |c| c.email.match( /deletable/ )  }
  custs.each { |c| c.delete }
end

Upvotes: 3

djb
djb

Reputation: 668

I'm coming a little late to the game, but check out StripeMock. It is made specifically for this purpose.

StripeMock.start
Stripe::Customer.create({
  id: '123',
  email: '[email protected]',
  card: 'void_card_token',
  subscriptions: {
    data: [{
      plan: {
        name: 'Some Subscription Plan',
        currency: 'usd',
        amount: 1000
      },
      start: 1410408211,
      status: 'active',
      current_period_start: 1410408211,
      current_period_end: 1413000211
    }]
  }
})

test... test... test...
StripeMock.stop

Upvotes: 5

Codasaurus
Codasaurus

Reputation: 860

It's difficult to really test an integration without making your tests as realistic as possible. Therefore, I suggest that you let stripe create the test customer accounts, but make use of the webhooks to automatically delete them.

Specifically, you would handle the customer.created event. If event->livemode == false, send a request back to the api to delete the customer.

This way, you run through the entire process as you test, while only keeping customers that were created in live mode.

Upvotes: 1

dwhalen
dwhalen

Reputation: 1925

This is typically done with a tool like WebMock which intercepts the HTTP request and returns a response you provide. The VCR gem makes this process easier by recording and replaying HTTP requests using WebMock (or optionally FakeWeb) under the hood. There is a Railscast on VCR, but it is a bit out of date. Take care not to record any sensitive data (i.e. api keys) by using the filter_sensitive_data configuration option.

Upvotes: 2

Related Questions