How to mock Stripe::Charge.retrieve using Stripe Mock gem

I created customer, made a subscription on the behalf of the customer. Along with that I generated invoices and expected in the RSpec example like


it 'should make sure reward amount in transaction history is equal \
    to amount added by the supporter.' do
  invoice = customer.invoices.first
  charge = Stripe::Charge.create(customer:, amount:, currency: 'usd').to_h
  charge[:id] = invoice.charge
  charge =
  allow(Stripe::Charge).to receive(:retrieve).and_return(charge) # A mock object would be more useful here.
  expect(TransactionHistory.first.added_rewards).to eq(

Issues with Stripe : Mock and solutions

undefined method `api_key=’ for StripeMock:Module

Looks like we dont need to pass API_KEYs if everything we are going to mock everything.

Stripe::InvalidRequestError: No such plan: thepact_monthly

Create a mock plan like

plan_params = {:id => 'thepact_monthly',
               :name => 'StripeMock Default Plan ID',
               :amount => 1900,
               :currency => 'usd',
               :interval => 'month'}

Stripe::InvalidRequestError: You must supply a valid card xoxo

You need to pass ‘source’ attribute as well

subscription = customer.subscriptions.create(
        plan: 'thepact_monthly',
        source: stripe_helper.generate_card_token({name: 'Shiva'})