在 Rails 3 和 Rspec 中存根链式查询 [英] Stubbing Chained Queries in Rails 3 and Rspec

查看:20
本文介绍了在 Rails 3 和 Rspec 中存根链式查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试我拥有的基于一系列其他范围的范围.(下面的public_stream").

I'm trying to test a scope I have that is based upon a chain of other scopes. ("public_stream" below).

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

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

However, I continue to receive this error:

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

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

It seems includes_values is an instance variable of the ActiveRecord::Relation object, but when I try to stub it, I still receive the same error. I was wondering if anyone had experience with stubing Rails 3's new chained queries? I can find a bunch of discussion over 2.x's find hash, but nothing on how to test what's current.

推荐答案

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

I use rspec's stub_chain for this. You might be able to use something like:

some_model.rb

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}

这篇关于在 Rails 3 和 Rspec 中存根链式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆