integrallis / stripe_event

Stripe webhook integration for Rails applications.
https://rubygems.org/gems/stripe_event
MIT License
844 stars 104 forks source link

rspec / capybara? #16

Closed MattRogish closed 11 years ago

MattRogish commented 11 years ago

I'm curious - is there a way to test the webhooks in my app? I basically copied your stripe_event_spec and used the fixtures, which "works" but feels terrible. Is there a DRY'er way?

Thanks,

Matt

invisiblefunnel commented 11 years ago

If you're using RSpec, request specs are a good way to test the behavior of webhook handlers. Only the id parameter needs to be passed to the post request because StripeEvent discards the rest.

# spec/requests/stripe_webhooks_spec.rb
require "spec_helper"
describe "POST /your-webhook-path" do
  context "charge.failed" do
    it "emails the user" do
      # setup models
      # stub api request to retrieve the event object from Stripe (I like webmock or vcr+webmock)
      post "/your-webhook-path", :id => "evt_some_charge_failed_event"
      # assert that the system behaved appropriately
    end
  end
end

Here's a handy curl command for manual testing.

$ curl -X POST -H "Content-type: application/json" -H "Accept: application/json" -d '{"id":"evt_REPLACEME"}' localhost:3000/your-webhook-path

I hope this helps. Let me know if I can expand on or clarify anything.

MattRogish commented 11 years ago

Sorry, didn't see that - but it works great. THANKS!

invisiblefunnel commented 11 years ago

Awesome. I'm planning to put the wiki together for this repo at some point. I'll be sure to mention this.