2013-10-07 6 views
0

Во-первых, я новичок, когда дело доходит до написания тестов в Rails. Благодарим вас за ваше терпение.Rails/Rspec: Сложные тесты времени написания

Вот мой класс:

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. Любая помощь будет очень признательна!

+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

Можете ли вы объяснить метод «двойной»? – dennismonsewicz

+0

double дает вам насмешливый объект. Это то же самое, что и макет («объект»)/заглушка («объект»). Это должно помочь вам понять -> https://relishapp.com/rspec/rspec-mocks/docs – usha

Смежные вопросы