在Rails 3和Rspec中连接链式查询

Lev*_*lum 14 activerecord rspec stub ruby-on-rails-3

我正在尝试测试我所拥有的基于一系列其他范围的范围.(下面的"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) }
Run Code Online (Sandbox Code Playgroud)

使用这样的测试:

    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
Run Code Online (Sandbox Code Playgroud)

但是,我继续收到此错误:

Failure/Error: Entry.public_stream(@user)
undefined method `includes_values' for #<Entry:0xd7b7c0>
Run Code Online (Sandbox Code Playgroud)

似乎includes_values是ActiveRecord :: Relation对象的实例变量,但是当我尝试存根时,我仍然收到相同的错误.我想知道是否有人有经验固定Rails 3的新链式查询?我可以找到关于2.x的查找哈希的一堆讨论,但没有关于如何测试当前的内容.

ast*_*ohn 21

stub_chain为此使用了rspec .您可以使用以下内容:

some_model.rb

scope :uninteresting, :conditions => ["category = 'bad'"],
                      :order => "created_at DESC"
Run Code Online (Sandbox Code Playgroud)

调节器

@some_models = SomeModel.uninteresting.where(:something_else => true)
Run Code Online (Sandbox Code Playgroud)

规范

SomeModel.stub_chain(:uninteresting, :where) {mock_some_model}
Run Code Online (Sandbox Code Playgroud)