我正在尝试测试基于其他范围链的范围。 (下面的“ 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>


似乎include_values是ActiveRecord :: Relation对象的实例变量,但是当我尝试对它进行存根处理时,我仍然收到相同的错误。我想知道是否有人对存入Rails 3的新链式查询有经验?我可以在2.x的find哈希中找到很多讨论,但是没有关于如何测试最新内容的讨论。

最佳答案

我为此使用rspec的stub_chain。您可能可以使用类似:

some_model.rb

scope :uninteresting, :conditions => ["category = 'bad'"],
                      :order => "created_at DESC"


控制者

@some_models = SomeModel.uninteresting.where(:something_else => true)


规格

SomeModel.stub_chain(:uninteresting, :where) {mock_some_model}

关于activerecord - 在Rails 3和Rspec中 stub 链式查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4057221/

10-12 15:15