ruby on rails - How to test mongoid scopes? -
ruby on rails - How to test mongoid scopes? -
i defined next user class scopes :
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
if utilize next rspec test :
describe 'scopes' let(:admin1) { fabricate(:admin) } let(:admin2) { fabricate(:admin) } describe 'recents' 'should homecoming admins recent older' user.all_admins.recents.should eq([admin1, admin2]) end end end
i next fail message :
got: #<mongoid::criteria selector: {"role"=>:admin}, options: {:sort=>{"created_at"=>-1}}, class: user, embedded: false>
so how test scope ?
mongoid lazy loads, do:
user.all_admins.recents.to_a.should eq([admin1, admin2])
side notes:
you should create scopes lambdas, norm rails4
i got issues symbol type in mongo (during migrations), i'd rather utilize strings
i'm pretty sure test fail since objects not created in db (let
not evaluated until it's called, replace let!
)
ruby-on-rails rspec mongoid
Comments
Post a Comment