Stripe Recurring Billing Part 3 : Integration Testing using Capybara

Jeff Thomas has written a blog post on Integration tests with Stripe. I followed his instructions, but I was not able to get it working. So, I had to use just Capybara with RSpec. In this article you will learn how to check the API credentials using a rake task and how to write integration tests using Capybara for testing the subscription feature using Stripe API.

Credentials Check


Here is a rake task that you can use to make sure that the Stripe credentials are defined.

namespace :stripe do
  task :credentials_check => :environment do
    message = <<ERROR 
Stripe Credentials is not set. Define the following Stripe Environment variables.

export STRIPE_PUBLISHABLE_KEY='pk_test_your publishable key' 
export STRIPE_SECRET_KEY='sk_test_your secret key'

Check config/initializers/stripe.rb for more details on this works.
ERROR

    if Rails.configuration.stripe[:secret_key].blank?
      puts message
    else
      begin
        Stripe::Account.retrieve
        puts "Stripe Secret Key is defined properly"
      rescue  Stripe::AuthenticationError => e
        puts "Stripe Secret Key is not correct"
      end
    end
  end

end

Ideally, the API should provide something similar to ping to verify the credentials. Unfortunately, there is no API for credentials check. The closest thing that I could find is retrieving the Stripe account details for the website owner who wants to charge their customers using Stripe API.

Integration Tests using Feature Specs


Step 1

Add the following gems to the Gemfile.

group :test do
  gem 'capybara'
  gem 'selenium-webdriver'
end

Step 2

Run bundle.

Step 3

Create a features directory under spec folder. Create a file subscribe_spec.rb under the features directory with the following contents.

require 'rails_helper'

feature 'Subscription' do
  scenario 'Customer subscribes to Gold plan', js: true do
    visit "/subscriptions/new"
    fill_in "Card Number", with: '4242424242424242'
    page.select '10', from: "card_month"
    page.select '2029', from: 'card_year'

    click_button 'Subscribe Me'
    expect(page).to have_content('You have been subscribed to Gold.')
  end
end

Step 4

Run the feature spec.

$rspec spec/features/subscribe_spec.rb 

If the feature tests are not running on the browser, you can check the default driver in rails console:

$rails c test
Loading test environment (Rails 4.2.5)
 > Capybara.default_driver
 => :rack_test 

You can change it to selenium, poltergeist or anything else in rails_helper.rb file:

# Default driver is :rack_test
Capybara.default_driver = :selenium

Now the tests will run on the browser using the selenium-webdriver. You can download the entire source code for this project from bitbucket: git@bitbucket.org:bparanj/striped.git.

References


  1. Feature Spec
  2. Stripe Ruby Mock
  3. Integration tests with Stripe
  4. Capybara Docs


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.