Reputation: 43153
I have the following model:
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
I want to write specs for the scopes, to make sure they're reliably working. My problem is how to deal with the Photo
model. Photos have a lot of validations on them, for instance they must belong_to a User
. So, I can write a working spec for these queries like so:
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
However, as you can imagine this is sloooow. This makes a bunch of calls to the database. (1) to make two kueues, (2) to make a photo, (3) to make a user who owns the photo... and so on, down the line.
This seems like a simple enough problem, all I need is one join record between a photo and a kueue and I can test this really fast. So how would you go about mocking this interaction so you can test it faster and in better isolation?
PS: I'm using Rails 3.2.8, Squeel (hence the query syntax) and my model is called Kueue because Queue is a reserved word in Rails :)
Upvotes: 0
Views: 691
Reputation: 18845
i think that it would not make sense to mock in the context of scopes.
scopes are basically database-queries and you want to make sure they work with, you know, the database.
so if your problem is test-performance, then i would suggest:
OR
Upvotes: 2