stripe-ruby-mock /stripe-ruby-mock
A mocking library for testing stripe ruby
★ 998⑂ 744👁 27⑃ 610● 358▤ 6
+0
Daily
998
stars
744
forks
27
watchers
610
prs
358
issues
6
releases
README
stripe-ruby-mock
Homepage: https://github.com/stripe-ruby-mock/stripe-ruby-mock
Issues: https://github.com/stripe-ruby-mock/stripe-ruby-mock/issues
CHAT: https://gitter.im/rebelidealist/stripe-ruby-mock
REQUEST: Looking for More Core Contributors
This gem has unexpectedly grown in popularity and I've gotten pretty busy, so I'm currently looking for more core contributors to help me out. If you're interested, there is only one requirement: submit a significant enough pull request and have it merged into master (many of you have already done this). Afterwards, ping @gilbert in chat and I will add you as a collaborator.
Install
In your gemfile:
gem 'stripe-ruby-mock'
!!! Important
We have changelog. It's first attempt. Feel free to update it and suggest to a new format of it.
version 5.0.0 has breaking changes - we support stripe 5 and 5 and = 3.0.0
stripe 5 & 'my_plan', :amount = 1500)
The above line replaces the following:
plan = Stripe::Plan.create(
:id = 'my_plan',
:name = 'StripeMock Default Plan ID',
:amount = 1500,
:currency = 'usd',
:interval = 'month'
)
expect(plan.id).to eq('my_plan')
expect(plan.amount).to eq(1500)
end
end
The available helpers are:
ruby
stripe_helper.create_plan(my_plan_params)
stripe_helper.delete_plan(my_plan_params)
stripe_helper.generate_card_token(my_card_params)
For everything else, use Stripe as you normally would (i.e. use Stripe as if you were not using StripeMock).
Live Testing
Every once in a while you want to make sure your tests are actually valid. StripeMock has a switch that allows you to run your test suite (or a subset thereof) against Stripe's live test servers.
Here is an example of setting up your RSpec (2.x) test suite to run live with a command line switch:
ruby
RSpec 2.x
RSpec.configure do |c|
if c.filter_manager.inclusions.keys.include?(:live)
StripeMock.toggle_live(true)
puts "Running live tests against Stripe..."
end
end
With this you can run live tests by running rspec -t live
Here is an example of setting up your RSpec (3.x) test suite to run live with the same command line switch:
ruby
RSpec 3.x
RSpec.configure do |c|
if c.filter_manager.inclusions.rules.include?(:live)
StripeMock.toggle_live(true)
puts "Running live tests against Stripe..."
end
end
Mocking Card Errors
Ensure you start StripeMock in a before filter StripeMock.start
Tired of manually inputting fake credit card numbers to test against errors? Tire no more!
ruby
it "mocks a declined card error" do
Prepares an error for the next create charge request
StripeMock.prepare_card_error(:card_declined)
expect { Stripe::Charge.create(amount: 1, currency: 'usd') }.to raise_error {|e|
expect(e).to be_a Stripe::CardError
expect(e.http_status).to eq(402)
expect(e.code).to eq('card_declined')
}
end
Built-In Card Errors
ruby
StripeMock.prepare_card_error(:incorrect_number)
StripeMock.prepaTop issues
#795When will the new version be the released?21 comments · 2021-10-18#492Miss the "items" and "trial_period_days" parameters for create_subscription15 comments · 2017-11-24#467Including Stripe Signature header when testing with Rspec?12 comments · 2017-09-04#634undefined method `data' for {}:Hash on Stripe Source Create12 comments · 2019-08-01#284You must supply a valid card error12 comments · 2015-12-04#203Stripe bindings v1.20.2 break Stripe Mock12 comments · 2015-03-18#205wrong number of arguments (6 for 3..5)9 comments · 2015-03-24#733Can't pass in an ID for a new subscription8 comments · 2020-07-09
Discussions
#915Do we need a test for mocked data changes?0 comments · 2024-08-06#887When will the new version be the released?5 comments · 2023-10-14
Latest release
v4.0.0 v4.0.0 2024-08-06What's Changed - 905 Allow Stripe SDK v11 by @stevenharman - 830 Implement search API by @adamstegman - 848 Extending runtime dependency on Stripe gem from version 5 through 11 by @smakani - 893 Adds support for stripe-ruby v10 by @fabianoarruda Full Changelog: https://github.com/stripe-ruby-mock/stripe-ruby-mock/compare/final-old-3-1-0...v4.0.0