在Rails 3的和Rspec的打桩链式查询 [英] Stubbing Chained Queries in Rails 3 and Rspec

查看:143
本文介绍了在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) }

使用这样的测试:

Using a test like this:

    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 ::关系对象的实例变量,但是当我试图存根它,我仍然收到了同样的错误。我想知道是否有人曾与stubing Rails 3的新链接查询体验?我可以找到关于如何测试什么是目前的一帮讨论过2.X的查找哈希,但什么都没有。

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天全站免登陆