How to write integration tests for Stripe checkout on Rails?

前端 未结 5 1782
忘掉有多难
忘掉有多难 2020-12-14 02:24

I\'ve hit the wall trying to write an integration test for Stripe\'s checkout.js [ https://checkout.stripe.com/checkout.js ] for my Rails 3.2 app.

Stripe checkout wo

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 03:11

    I tried James's answer and modified for my current environment. Here is my code (system spec with Chrome headless):

    require 'rails_helper'
    
    describe 'Orders', type: :system do
      before do
        # Temporarily change default_max_wait_time to wait for Stripe response
        @old_wait_time = Capybara.default_max_wait_time
        Capybara.default_max_wait_time = 10
      end
    
      after do
        Capybara.default_max_wait_time = @old_wait_time
      end
    
      scenario 'Payment via Stripe', js: true do
        visit payment_path
        click_button 'Pay with Card'
    
        # Use VCR to avoid actual data creation
        VCR.use_cassette 'orders/payment_via_stripe' do
          expect(page).to have_css('iframe[name="stripe_checkout_app"]')
          stripe_iframe = all('iframe[name=stripe_checkout_app]').last
    
          Capybara.within_frame stripe_iframe do
            # Set values by placeholders
            fill_in 'Card number', with: '4242424242424242'
            fill_in 'MM / YY', with: '08/44'
            fill_in 'CVC', with: '999'
            # You might need to fill more fields...
    
            click_button 'Pay $9.99'
          end
    
          # Confirm payment completed
          expect(page).to have_content 'Payment completed.'
        end
      end
    end
    

    I am using:

    • selenium-webdriver 3.14.0
    • rspec-rails 3.8.0
    • capybara 3.7.1
    • stripe 3.26.0
    • rails 5.2.1
    • ruby 2.5.1
    • vcr 4.0.0
    • webmock 3.4.2
    • Chrome 69

    My app is built according to https://stripe.com/docs/checkout/rails .

提交回复
热议问题