Stubbing Chained Queries in Rails 3 and Rspec

馋奶兔 提交于 2019-11-30 06:46:58

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

some_model.rb

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

Controller

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

spec

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

Same Answer as above.

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

Rspec 3 version:

allow(SomeModel).to receive_message_chain(:uninteresting).and_return(SomeModel.where(nil))

Reference:

https://relishapp.com/rspec/rspec-mocks/docs/method-stubs/stub-a-chain-of-methods

First off, you probably should not be testing built-in Rails functionality.

You should only be writing unit tests for code that you have written yourself (which should be second-nature if you practice TDD) – Rails ships with its own comprehensive suite of unit tests for its built-functionality – there is no point in replicating this.

As far as the error being thrown, I think that your issue is on this line:

@chain.should_receive(:limit).with(15).and_return(Factory(:entry))

You are expecting the chain to return a Factory, which would effectively be an instance of ActiveRecord, but in actuality every relation returns yet another ActiveRecord::Relation.

Thus, your expectation itself is incorrect, and may indeed be causing the error that's being thrown.

Keep in mind, that scopes don't actually return the records you expect, until you explicitly iterate over them. Also, the relation's records will never return a single record. They will always either return an empty array or an array with records.

Try passing on the Arel, as it might be the case that it scopes are going missing.

it "should use the public, without_user, completed, and limit scopes" do
  @chain = Entry
  @chain.should_receive(:public).and_return(@chain.public)
  @chain.should_receive(:without_user).with(@user).and_return(@chain.without_user(@user))
  @chain.should_receive(:completed).and_return(@chain.completed)
  @chain.should_receive(:limit).with(15).and_return(Factory(:entry))

  Entry.public_stream(@user)
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!