在 Rails 3 和 Rspec 中存根链式查询
我正在尝试测试我拥有的基于一系列其他范围的范围。 (下面的“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>
似乎includes_values 是 ActiveRecord::Relation 对象的实例变量,但是当我尝试存根它时,我仍然收到相同的错误。我想知道是否有人有过对 Rails 3 新的链式查询进行存根的经验?我可以找到一堆关于 2.x 的 find hash 的讨论,但没有找到关于如何测试当前内容的讨论。
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>
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用 rspec 的
stub_chain
来实现此目的。您也许可以使用类似的东西:some_model.rb
控制器
规范
I use rspec's
stub_chain
for this. You might be able to use something like:some_model.rb
Controller
spec
与上面的答案相同。
Rspec 3 版本:
参考:
https:// /relishapp.com/rspec/rspec-mocks/docs/method-stubs/stub-a-chain-of-methods
Same Answer as above.
Rspec 3 version:
Reference:
https://relishapp.com/rspec/rspec-mocks/docs/method-stubs/stub-a-chain-of-methods
首先,您可能不应该测试内置的 Rails 功能。
至于抛出的错误,我认为您的问题在于这一行:
您期望链返回一个
Factory
,它实际上是ActiveRecord
的实例,但实际上 每个关系都会返回另一个ActiveRecord::关系
。因此,您的期望本身是不正确的,并且可能确实导致了抛出的错误。
请记住,范围实际上不会返回您期望的记录,直到您明确地迭代它们。此外,关系的记录永远不会返回单个记录。它们总是要么返回一个空数组,要么返回一个包含记录的数组。
First off, you probably should not be testing built-in Rails functionality.
As far as the error being thrown, I think that your issue is on this line:
You are expecting the chain to return a
Factory
, which would effectively be an instance ofActiveRecord
, but in actuality every relation returns yet anotherActiveRecord::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.
尝试传递 Arel,因为它的瞄准镜可能会丢失。
Try passing on the Arel, as it might be the case that it scopes are going missing.