2016-08-11 17 views
0

我有一個MongoId模式是這樣的:如何使用Mongoid驅動程序在Rails測試環境中啓用Mongo索引?

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

    field :username 

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

我想寫一些單元測試,但我想這個指數,而在我的測試套件創建這樣的模型來實現。

似乎索引默認情況下未啓用。

任何線索?

P.S.我正在使用mongoid gem的工具Rails 4:5.1.3

回答

0
Acme::Account.create_indexes 

將創建索引。所以你可以在你的測試中調用它。例如在before :eachbefore :suite區塊中。

+0

儘管我不明白爲什麼你在'before:suite'塊中有這麼複雜的代碼,我發現你的答案有助於解決我的問題。我會推薦一個編輯。 –

相關問題