Rspec Mongoid scope works in development not in tests

I have a scope that I apply to a Mongoid object in rails, which works fine when developing, but not when running tests. Actually this does not work at all in tests. This is an embedded document.

Parent:

class Person include Mongoid::Document def self.with_appointments where(:appointments.not => { '$size' => 0 }) end embeds_many :appointments, store_as: 'Appointments', class_name: 'Appointment' end 

Nested child:

 class Appointment include Mongoid::Document embedded_in :person end 

When I run tests against my model, it returns to Persons whether they have meetings or not. If I run the same from my controller in development against the database, it filters out people with empty appointments.

What am I missing?

Before asking me to change the database setup -

  • I have no control over the data structure
  • Suppose I do not have write access to the database
+6
source share
1 answer

Check your GemFile, I bet that the Mongoid is configured to run only in development:

Try switching this

 gem 'mongoid', group: :development 

to that

 gem 'mongoid' 
0
source

Source: https://habr.com/ru/post/987308/


All Articles