我用范围定义了以下用户类:
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>
那么我该如何测试这个范围呢?