Stripe Recurring Billing Part 5 : Testing using Stripe Ruby Mock Gem


Testing Stripe related code becomes difficult because of too much stubbing code. To make this easier, we will use stripe-ruby-mock gem. This gem does not make any network calls, so we don't hit stripe's servers. So, our tests run faster.


Step 1

Add the stripe-ruby-mock gem to Gemfile and run bundle install.

group :test do
  gem 'capybara'
  gem 'selenium-webdriver'
  gem 'stripe-ruby-mock'

Step 2

Here is the tests that checks if we are saving the subscription details in our database and if the customer subscribed to our gold plan.

require 'stripe_mock'
require 'rails_helper'

describe StripeGateway do
  let(:stripe_helper) { StripeMock.create_test_helper }
  let(:plan) { stripe_helper.create_plan(:id => 'gold', :amount => 1500) }

  before { StripeMock.start }
  after { StripeMock.stop }

  it 'creates a stripe customer in our database' do
    sg =

    expect do
      subscription = sg.create_subscription('test-email', stripe_helper.generate_card_token, change{Subscription.count}.by(1)

  it 'customer should be subscribed to gold plan' do
    sg =
    subscription = sg.create_subscription('test-email', stripe_helper.generate_card_token,

    expect(subscription.plan_name).to eq('gold')


Step 3

Here is the spec/controllers/subscriptions_controller_spec.rb:

require 'rails_helper'

describe SubscriptionsController do

  it 'should delegate creating stripe customer and subscription record to stripe gateway' do
    stripe_gateway = instance_double('StripeGateway')    
    allow(StripeGateway).to receive_messages(:new => stripe_gateway)
    allow(stripe_gateway).to receive(:create_subscription).with('', '1', 'gold')

    post :create, { stripeToken: '1', plan_name: 'gold'}

  it 'should initialize plan name' do
    get :new, {plan_name: 'gold'}

    expect(assigns(:plan_name)).to eq('gold')

Step 4

Here is the test for the subscription model.

require 'rails_helper'

describe Subscription do
  it 'should display plan name in human readable form' do
    subscription = 'gold')

    expect(subscription.plan_display_name).to eq('Gold')

  it 'subscription is not complete when the customer is not in stripe system' do
    subscription =

    expect(subscription).not_to be_complete

  it 'subscription is complete when the customer is in the strip system' do
    subscription = 1)

    expect(subscription).to be_complete


This spec uses the latest stubbing and mocking syntax for RSpec mocks version 3.0.4. See the link in the reference section for more details. You can checkout the code for this article by cloning 8aa4a8f commit of git repository


Notable Changes in RSpec 3
Stripe Ruby Mock Gem Home Page
RSpec Builtin Matchers

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.