Jeff Thomas

My Reflections while on the pale blue dot

Integration tests with Stripe

September 18, 2014

I recently added Stripe integration to a web site and wanted to share my experience.

I started with an integration test, in accordance with an outside-in TDD approach.

As a good netcitizen, I wanted to refrain from hitting the Stripe servers with every test run, so I started to mock the Stripe API.

My first reaction was to reach for the handy vcr gem. Similar to a vcr, the vcr gem records the first response from an API and then uses this cached response in subsequent test runs.

I soon realized the vcr gem is designed to mock only server side http requests and not client side browser requests.

When using stripe.js, the browser first sends the request to the stripe api to get a token that represents the credit card information.

The request is then forwarded to the server where a separate request is made for the payment to the Stripe API using the tokenized credit card value.

My initial search for a gem to handle browser based (aka javascript) http requests uncovered the puffing billy gem. If nothing else, the name was cool.

In the end I decided to use the stripe-ruby-mock gem. This gem is specifically designed for stubbing the Stripe API, including a way to start up a API mock server for integration tests.

The documentation is good, but here are the specific steps I took to get this running. I'm using rspec.

# Gemfile
group :test do  
  gem 'stripe-ruby-mock'
  gem 'thin' # default for mock server

# rails_helper.rb

require 'thin' require 'stripe_mock'


Here is the integration test:


Here is the mock server in action. You can see the server daemon starts before the test and then ends after the test.