0
我定义了以下用户类别与范围:如何测试mongoid范围?
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Search
# SCOPES
scope :all_admins, where(role: :admin)
scope :recents, order_by(created_at: :desc)
scope :olders, order_by(created_at: :asc)
field :role, type: Symbol
end
如果我用下面的RSpec的测试:
describe 'scopes' do
let(:admin1) { Fabricate(:admin) }
let(:admin2) { Fabricate(:admin) }
describe 'recents' do
it 'should return the admins from most recent to older' do
User.all_admins.recents.should eq([admin1, admin2])
end
end
end
我得到以下失败消息:
got: #<Mongoid::Criteria
selector: {"role"=>:admin},
options: {:sort=>{"created_at"=>-1}},
class: User,
embedded: false>
怎么可能我测试这个范围?
我不知道如果没有有高架即使没有必要,也可以将范围作为lambda表达式。你有没有看到有关这个问题的任何事情? – ksol 2013-02-21 09:28:56
伟大的:to_a完成了这项工作。让!也参与了解决方案的一部分,因为这些对象不是用let创建的。 – obo 2013-02-26 08:10:07