You have 7 different paths to test for your process method. I am writing the test for two scenarios and leaving the rest for you to try. Also please note that my tests are under the assumption that the other methods process calls are tested separately.
There could be minor syntax/errors here because its untested. But it will give you an idea of how to test the process method
describe "Process" do
it "should do nothing if the event is not accepted" do
webhook = Webhook.new
webhook.stub(:event_accepted?).and_return(false)
InvoiceMailer.should_not_receive(:payment_succeeded_email)
InvoiceMailer.should_not_receive(:payment_failed_email)
webhook.process
end
it "should send a payment succeeded email if the event type is success" do
customer = FactoryGirl.create(:user)
webhook = Webhook.new({"type": "invoice.payment_succeeded", "data": {"object": {"id": 1, "customer": customer.id}}})
Stripe::Invoic.should_receive(:retrieve).with("1").and_return(invoice = double("invoice", :amount_due => 20))
InvoiceMailer.should_receive(:payment_succeeded_email).with(customer, invoice)
webhook.process
end
it "should do nothing if the event type is success but the invoice due is zero" do
end
it "should log when there is an exception in processing the successful payment" do
end
it "should expire the user if the subscription is deleted" do
customer = FactoryGirl.create(:user)
webhook = Webhook.new({"type": "customer.subscription.deleted", "data": {"object": {"id": 1, "customer": customer.id}}})
User.stub(:find_by_customer_id).with(customer.id).and_return(customer)
customer.should_receive(:expire!)
webhook.process
end
it "should do nothing if the subscription is deleted and the user is invalid" do
webhook = Webhook.new({"type": "customer.subscription.deleted", "data": {"object": {"id": 1, "customer": customer.id}}})
User.stub(:find_by_customer_id).with(customer.id).and_return(nil)
User.any_instance.should_not_receive(:expire!)
webhook.process
end
it "should send a failure email if the payment was not successful" do
end
end