p.matsinopoulos
p.matsinopoulos

Reputation: 7810

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.

Upvotes: 0

Views: 538

Answers (2)

Weekend Baf
Weekend Baf

Reputation: 307

In my case I configure the index like this:

RSpec.configure do |config|
  config.before(:all, index: true) do
    Model.remove_indexes
    Model.create_indexes
  end
end

Add it to test cases are trying with index.

context 'indexing', index: true do;end

I think it handy because almost my test cases that not really need index

Upvotes: 0

kuadrosx
kuadrosx

Reputation: 411

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.

Upvotes: 0

Related Questions