A Stripe fake so that you can avoid hitting Stripe servers in tests.
Clone or download
cpytel Return error on native or zero amounts
When you pass a negative or 0 as an amount to Stripe it results in an
error being returned. On our app that uses FakeStripe, we had a bug
that was sending negative amounts to be charged and this was not caught
by the specs because FakeStripe was happily taking this request.

This introduces this error state to match the behavior of the Stripe
API. If a zero or negative amount is attemoted to be charged, an error
will be returned.
Latest commit f4efd0c Aug 8, 2018


fake_stripe, a Stripe fake

This library is a way to test Stripe code without hitting Stripe's servers. It uses Capybara::Server and Webmock to intercept all of the calls from Stripe's Ruby library and returns JSON that the Stripe library can parse.



Add the fake_stripe Gem to the :test group in your Gemfile:

# Gemfile
group :test do
  gem 'fake_stripe'

Remember to run bundle install.

Stripe settings

Set the STRIPE_JS_HOST constant in an initializer:

# config/initializers/stripe.rb
Stripe.api_key = ENV['STRIPE_API_KEY']

unless defined? STRIPE_JS_HOST
  STRIPE_JS_HOST = 'https://js.stripe.com'

Include the Stripe JavaScript in your application template.

If you're using Stripe.js v1:

# app/views/layouts/application.html.erb
<%= javascript_include_tag "#{STRIPE_JS_HOST}/v1/" %>

Or if you're using Stripe.js v2:

# app/views/layouts/application.html.erb
<%= javascript_include_tag "#{STRIPE_JS_HOST}/v2/" %>

When the test suite runs fake_stripe will override the address for STRIPE_JS_HOST and serve up a local version of Stripe.js.

In Tests

Require the library in your spec support:

# spec/support/fake_stripe.rb
require 'fake_stripe'

RSpec.configure do |config|
  config.before(:each) do


Please see CONTRIBUTING.md for more details.