使用 scalatest
和 Casbah,我创建了一个测试,将一堆文档加载到 Mongo 中,然后断言 collection.count() > 0
。
val collection = MongoConnection()(MY_DB)(MY_COLLECTION)
collection.dropCollection // clear out any docs from previous test run
insert200DocumentsIntoMongo() // inserts 200 docs into the same DB and collection
assert(collection.size > 0)
对于多个测试,scalatest
会抛出断言不正确的异常。
但是,测试失败后,我可以在 Mongo shell 中清楚地看到,根据上面的“MY_DB”和“MY_COLLECTION”,200 个文档被添加到 Mongo 数据库的集合中。
>db.test.count()
200
我很困惑为什么这个断言会失败,因为 Mongo shell 表明集合中有 200 个文档。
此外,我尝试使用此 post 删除整个数据库,但断言仍然失败。
最佳答案
尝试改变 mongo 写关注:
collection.setWriteConcern(WriteConcern.FsyncSafe)
关于mongodb - Mongo Shell 显示已添加文档,但 Scalatest 中断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19107126/