【发布时间】:2012-11-06 20:43:45
【问题描述】:
我有以下型号:
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) 制作两个 kueues,(2) 制作一张照片,(3) 制作一个拥有该照片的用户……等等。
这似乎是一个很简单的问题,我只需要一张照片和一个 kue 之间的连接记录,我可以很快地测试这个。那么,您将如何模拟这种交互,以便更快、更好地进行测试呢?
PS:我使用的是 Rails 3.2.8,Squeel(因此是查询语法),我的模型称为 Kueue,因为 Queue 是 Rails 中的保留字 :)
【问题讨论】:
标签: ruby-on-rails rspec refactoring tdd bdd