我正在尝试测试我拥有的基于一系列其他范围的范围。(下面的“public_stream”)。
scope :public, where("entries.privacy = 'public'")
scope :completed, where("entries.observation <> '' AND entries.application <> ''")
scope :without_user, lambda { |user| where("entries.user_id <> ?", user.id) }
scope :public_stream, lambda { |user| public.completed.without_user(user).limit(15) }
使用这样的测试:
it "should use the public, without_user, completed, and limit scopes" do
@chain = mock(ActiveRecord::Relation)
Entry.should_receive(:public).and_return(@chain)
@chain.should_receive(:without_user).with(@user).and_return(@chain)
@chain.should_receive(:completed).and_return(@chain)
@chain.should_receive(:limit).with(15).and_return(Factory(:entry))
Entry.public_stream(@user)
end
但是,我继续收到此错误:
Failure/Error: Entry.public_stream(@user)
undefined method `includes_values' for #<Entry:0xd7b7c0>
似乎 includes_values 是 ActiveRecord::Relation 对象的实例变量,但是当我尝试存根它时,我仍然收到相同的错误。我想知道是否有人对 Rails 3 的新链式查询有经验?我可以找到一堆关于 2.x 的 find hash 的讨论,但没有关于如何测试当前内容的内容。