2016-09-12 4 views
0

私はRSpecを初めて使い、モックでのテスト方法に苦しんでいます。ウェブフックが入って来たときに 。これは基本的に呼ばれている。複雑なケースのRspec

class InvoiceCreated 
    def call(event) 
    invoice = event.data.object 

    # NOTE: Skip if the invoice is closed. 
    if invoice.closed == false 
     stripe_customer = invoice.customer 
     payment_account = PaymentCardAccount.find_by(stripe_customer_id: stripe_customer) 
     card_invoice = Invoice.find_card_invoice_in_this_month_within(payment_account: payment_account) 

     card_invoice.process_invoice_items(stripe_customer: stripe_customer, 
             event_invoice_id: invoice.id) 
     card_invoice.process!(:pending, id: invoice.id) 
    end 
    end 
end 

私はモックを使用して、以下の2行のコードをテストするために呼び出すAPIを防ぐのが大好きです。

card_invoice.process_invoice_items(stripe_customer: stripe_customer, 
            event_invoice_id: invoice.id) 

    card_invoice.process!(:pending, id: invoice.id) 

モックを使用するにはどうすればよいですか?

+0

どのrspecのバージョンを使用していますか? – hakcho

+0

私は 'rspec-rails'、 '〜> 3.4.2'を使用します。 – Tosh

+0

"モック"と言うと、実際にここで何を達成しようとしていますか? 'NoMethodError:Undefined method process_invoice_items'のようなエラーで失敗することはありません。または、実際に請求書を処理するWeb API呼び出しをスタブアウトしたいのですが、そうでなければこの機能の「完全なテスト」を実行しますか?このテストは何を達成しようとしていますか? (おそらく、上記のポストに現在のテストを含めることができますか?) –

答えて

1

expect_any_instance_ofを使用して、Invoice#process_invoice_itemsInvoice#processを正しいパラメータで呼び出すことを確認できます。彼らがどのように呼び出されているか気にしない場合は、それらをスタブし、allow_any_instance_ofを使用してください。より詳細な例について

expect_any_instance_of(Invoice).to receive(:process_invoice_items) 
expect_any_instance_of(Invoice).to receive(:process!) 

See here、あなたが実際にそれで何ができるか見るためにrspec-mockの基本的な部分を通ってました。

関連する問題