mongodbunit-testingruby-on-rails-4mongoidmongoid5

How to enable Mongo indexes in Rails test environment with Mongoid driver?


I have a MongoId model like this:

module Acme
  class Account
    include Mongoid::Document
    include Mongoid::Timestamps

    field :username

    index({'username': 1}, {unique: true})
  end
end

I want to write some unit tests but I want this index to be enabled while creating such models in my test suite.

It seems that index is not enabled by default.

Any clue?

P.S. I am working on Rails 4, with mongoid gem: 5.1.3.


Solution

  • Acme::Account.create_indexes
    

    will create the indexes. So you can call that in your test. For example in a before :each or before :suite block.