2013-04-16 82 views
1

這些是我的Cashout模型的一些代碼。我可以使用rspec測試所有模型,但我不知道如何測試這些示波器。如何用rspec測試示波器

cashout.rb

class Cashout < ActiveRecord::Base 

belongs_to :partner 

scope :add_virtual_columns, select(
    "cashouts.*, 
    (SELECT SUM(c.amount) FROM cashouts c WHERE c.partner_id = cashouts.partner_id) as total_paid_amount, 
    (
     (
     CASE 
     WHEN (SELECT partner_type FROM partners WHERE cashouts.partner_id = partners.id) = 'administrator' 
     THEN 
      (
      CASE 
       WHEN (SELECT SUM(cr.partner_profit) FROM contact_records cr WHERE cr.partner_id in (SELECT id FROM partners WHERE company_id in (SELECT company_id FROM partners WHERE id = cashouts.partner_id))) IS NULL 
       THEN 0 
       ELSE (SELECT SUM(cr.partner_profit + cr.company_profit) FROM contact_records cr WHERE cr.partner_id in (SELECT id FROM partners WHERE company_id in (SELECT company_id FROM partners WHERE id = cashouts.partner_id))) 
      END 
      ) 
     ELSE 
      (
      CASE 
      WHEN (SELECT SUM(cr.partner_profit) FROM contact_records cr WHERE cr.partner_id = cashouts.partner_id) IS NULL 
      THEN 0 
      ELSE (SELECT SUM(cr.partner_profit) FROM contact_records cr WHERE cr.partner_id = cashouts.partner_id) 
      END 
     ) 
     END 
    ) 
     - 
     (SELECT SUM(c.amount) FROM cashouts c WHERE c.partner_id = cashouts.partner_id) 
    ) as unpaid_amount" 
).group('cashouts.id') 
    scope :sort_by_total_paid_amount_asc, order("total_paid_amount ASC") 
    scope :sort_by_total_paid_amount_desc, order("total_paid_amount DESC") 
    scope :sort_by_unpaid_amount_asc, order("unpaid_amount ASC") 
    scope :sort_by_unpaid_amount_desc, order("unpaid_amount DESC") 
end 

你知道如何測試這些示波器使用RSpec和factorygirl?

回答

0

我的傾向是創建一些測試數據,並將範圍結果與預期發生的情況進行比較。 FactoryGirl可以幫助您創建基地Cashout模型,您可以在其上覆蓋amountpartner_id,然後生成相應的PartnerContactRecord模型。

對於後者範圍,你可以不喜歡

Cashout.sort_by_total_paid_amount_asc.to_sql.should == Cashout.order('total_paid_amount ASC').to_sql