0
我有以下型號:Rspec的嘲弄一個相關記錄的測試查詢
class Kueue < ActiveRecord::Base
attr_accessible :name, :user_id
belongs_to :user
has_and_belongs_to_many :photos
scope :empty, includes(:photos).where{ photos.id.eq(nil) }
scope :with_photos, includes(:photos).where{ photos.id.not_eq(nil) }
end
我想寫規範的範圍,以確保他們工作可靠。我的問題是如何處理Photo
模型。照片對它們有很多驗證,例如它們必須屬於User
。所以,我可以寫一個工作規範這些查詢,像這樣:
describe "queries" do
before :each do
@empty = Fabricate(:kueue)
@full = Kueue.new :name => "Full Queue"
@full.photos << Fabricate(:photo)
@full.save
end
it "should find empty queues" do
Kueue.empty.should_not include(@full)
end
it "should find queues with photos" do
Kueue.with_photos.should_not include(@empty)
end
end
然而,正如你能想象這是sloooow。這使得一堆對數據庫的調用。 (1)製作兩個kueue,(2)製作照片,(3)製作擁有照片的用戶......等等。
這似乎是一個足夠簡單的問題,我需要的是一張照片和一張kueue之間的一個連接記錄,我可以測試這個速度非常快。那麼你如何去嘲笑這種交互作用,以便更快速地測試它並更好地隔離?
PS:我使用Rails 3.2.8,Squeel(因此查詢語法)和我的模型被稱爲Kueue因爲隊列是Rails的一個保留字:)
Phoet,非常感謝你爲此sugg estion。我不得不隨身帶着一些回調,所以有可能在測試環境中保存而不用驗證,但這*天文*提高了我的測試速度。主要的榮譽! – Andrew