2013-10-07 4 views
0

먼저 Rails에서 테스트 작성에 관해서 newb입니다. 양해 해 주셔서 감사합니다. 그것은 #process 방법에 대한 테스트를 작성하는 것을 시도에 올 때 나는 약간의 손실입니다Rails/Rspec : 어려운 테스트 시간 쓰기

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 

:

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 

것은 여기까지 내 테스트입니다 :

여기 내 클래스입니다. 어떤 도움이라도 대단히 감사하겠습니다!

+0

여기서 질문은 무엇입니까? – dax

+0

내 Webhook 클래스의 프로세스 메서드 주위에 테스트를 작성하는 방법 – dennismonsewicz

답변

1

프로세스 방법을 테스트하기 위해 7 가지 경로가 있습니다. 나는 두 가지 시나리오에 대한 테스트를 작성하고 나머지는 시험해보기 위해 남겨 둡니다. 또한 다른 테스트 프로세스 호출이 별도로 테스트된다는 가정하에 테스트가 진행됩니다.

테스트되지 않았기 때문에 사소한 구문/오류가있을 수 있습니다. 하지만 프로세스 방법을 테스트하는 방법에 대한 아이디어를 줄 것입니다.

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 
+0

모든 것에 대해 감사드립니다 !! 당신 정말 대단하네요! – dennismonsewicz

+0

'double' 방법을 설명 할 수 있습니까? – dennismonsewicz

+0

double은 조롱 된 객체를 제공합니다. 그것은 mock ("object")/stub ("object")와 같은 일입니다. 도움이됩니다. -> https://relishapp.com/rspec/rspec-mocks/docs – usha