Search code examples
rspecstripe-paymentsrspec-rails

RSpec, Capybara and Stripe failing on required product field


I am attempting to mock an API process that is being used with Stripe. In the app, a button is being used to call to the Charge API. I haven't seen this in any online resources, which all point to use the Plan API. The following runs alright except that it's looking for a required product field. Other tutorials such as Mocking Stripe with Rspec don't include the product parameter at all. I've also attempted to mock it using the value in Stripe's API Reference and other hardcoded values but it says product does not exist. I have tried working with this in either or both plan = statements

context "stripe processor" do
        let(:stripe_helper) { StripeMock.create_test_helper }

        it "creates a stripe plan" do
          plan = stripe_helper.create_plan(:id => '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

Note that I am using the stripe-ruby-mock gem. Thank you to anyone who may have ideas. Thanks.


Solution

  • In more recent API versions the product parameter refers to a Product ID, that product must exist before creating a Plan. See this api revision. For testing, you need to ensure that a given Product ID exists before creating the Plan or you'll get the error message that you see.

    It's possible that you could mock out both the Product and Plan with stripe-ruby-mock but it looks like the gem was built before those API changes and it may need modifications to work correctly here.