2013-04-08 77 views
2

這是我最喜歡的設計師模型軌反擊緩存和外鍵測試

favorite_designer.rb

class FavoriteDesigner < ActiveRecord::Base 
    belongs_to :user, :counter_cache => true 
    belongs_to :designer, :class_name => "User", :foreign_key => :designer_id 

    validates_presence_of :user_id 
    validates_presence_of :designer_id 

    validates_numericality_of :user_id, :unless => Proc.new{|f| f.user_id.blank?} 
    validates_numericality_of :designer_id, :unless => Proc.new{|f| f.designer_id.blank?} 
end 

我怎樣才能在RSpec的測試:counter_cache => true and :foreign_key => :designer_id與早該?

回答

1

您可以使用counter_cachewith_foreign_key早該的匹配:

it { should belong_to(:organization).counter_cache(true) } 

it { should have_many(:worries).with_foreign_key('worrier_id') } 

對於完整的文檔和例子看看:

Counter cache doc

Foreign key doc