![](/img/trans.png)
[英]Creating MongoDB indexes via Mongoid in non-Rails environment
[英]How to enable Mongo indexes in Rails test environment with Mongoid driver?
我有一個像這樣的MongoId模型:
module Acme
class Account
include Mongoid::Document
include Mongoid::Timestamps
field :username
index({'username': 1}, {unique: true})
end
end
我想編寫一些單元測試,但我希望在測試套件中創建此類模型時啟用此索引。
似乎默認情況下未啟用索引。
有什么線索嗎?
PS我正在開發Rails 4,使用mongoid gem: 5.1.3
。
Acme::Account.create_indexes
將創建索引。 因此,您可以在測試中調用它。 例如在before :each
或before :suite
塊中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.