Skip to content

Implementing a New Behavior

Gilbert edited this page Feb 21, 2015 · 4 revisions

First of all, thank you for your interest in contributing! If you find a missing Stripe behavior in StripeMock, feel free to create an issue and/or pull request for it. The following is helpful information on how to get started.

Running the Tests

First off, here is how you run the tests:

$ bundle install
$ bundle exec rspec

Live Testing

When developing on StripeMock, you want to write tests that reflect Stripe's actual behavior. Fortunately StripeMock has a way to verify your tests against Stripe's live test servers (albeit it's a bit crude). Here's how:

  1. Tag your it / context / describe with :live => true. For example:
describe "Validation", :live => true do
  let(:params) { stripe_helper.create_plan_params }
  # ...
  1. (Optional, but recommended) Open up spec/support/stripe_examples.rb and comment out all irrelevant it_behaves_like lines. This is useful because you don't want to waste time running so many slow tests that don't pertain to what you're working on. Don't accidentally commit this change!

  2. Run bundle exec rspec -t live

Contributing to StripeMock

When you ran your the test suite in your own project, it's likely you saw something like the following:

WARNING: Unrecognized method + url: [post /v1/unrecongnized_method]

but with unrecongnized_method replaced with the url of your desired, unimplemented api endpoint. This is your starting point; you'll see this be used in a request handler file. For example, to handle post /v1/customers, you would see this in the StripeMock codebase:

# lib/stripe_mock/request_handlers/customers.rb
module StripeMock
  module RequestHandlers
    module Customers

      def Customers.included(klass)
        klass.add_handler 'post /v1/customers', :new_customer
      end

      def new_customer(route, method_url, params, headers)
        # ...
      end
    end
  end
end

Modifying an Existing Resource Behavior

The resource request handlers are all in lib/stripe_mock/request_handlers/. Most likely you will need to modify one of these files.

lib/stripe_mock/instance.rb represents an instance of a stripe server. It's where most of the token logic lives, and where you should put helper methods used across resources.

Adding a New Resource

The Customer resource is the most complete example. Start by studying these files and then creating new files with customer replaced by card in the file names:

  • spec/shared_stripe_examples/customer_examples.rb: General specs for a resource. You probably won't need all of them
  • spec/support/stripe_examples.rb: Add your new spec file and behavior to these two methods
  • stripe_mock/request_handlers/customers.rb: The module that gets included in a StripeMock instance
  • stripe_mock/instance.rb: The engine that mocks stripe. Include your new module here
  • stripe_mock.rb: require your new request_handler here.

Miscellaneous

When developing make sure to add StripeMock.toggle_debug(true) when you need it. It'll give you helpful info on what's going on between the Stripe gem and the StripeMock instance.