まず、Rails でテストを作成することに関しては、私は初心者です。[前もって] お待ちいただきありがとうございます。
これが私のクラスです:
require 'json'
class Webhook
attr_accessor :customer_id, :response, :event_type
ACCEPTED_EVENTS = ["customer.subscription.deleted", "invoice.payment_succeeded", "invoice.payment_failed"]
def initialize(json = nil)
if json
@response = JSON.parse(json, symbolize_names: true)
@customer_id = @response[:data][:object][:customer]
@event_type = @response[:type]
@user = User.find_by_customer_id(@customer_id)
end
end
def event_accepted?
true if ACCEPTED_EVENTS.include?(@event_type)
end
def process
return unless event_accepted?
case @event_type
when "invoice.payment_succeeded"
begin
invoice = Stripe::Invoice.retrieve(@response[:data][:object][:id])
InvoiceMailer.payment_succeeded_email(@user, invoice).deliver if invoice.amount_due > 0
rescue => e
Rails.logger.info "An error as occurred! #{e}"
end
when "customer.subscription.deleted"
@user.expire! if @user
when "invoice.payment_failed"
InvoiceMailer.payment_failed_email(@user).deliver
end
end
end
これまでの私のテストは次のとおりです。
require 'spec_helper'
describe Webhook do
describe "instance methods" do
let(:webhook) { Webhook.new }
describe "#event_accepted?" do
it "returns true with a correct event_type" do
webhook.event_type = "customer.subscription.deleted"
webhook.event_accepted?.should be_true
end
it "returns false with an incorrect event_type" do
webhook.event_type = "foobar123"
webhook.event_accepted?.should be_false
end
end
end
end
#process
メソッドのテストを書こうとすると、少し戸惑います。どんな助けでも大歓迎です!