Search code examples
mongodbscalascalatestcasbah

Mongo Shell Shows Docs Added, yet Assert Fails in Scalatest


Using scalatest and Casbah, I created a test to load a bunch of documents into Mongo, and then assert that 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) 

For multiple tests, scalatest throws an exception that the assert is not true.

However, after the test fails, I can clearly see in the Mongo shell that 200 documents were added to the Mongo database's collection as per the above "MY_DB" and "MY_COLLECTION."

>db.test.count()
200

I'm confused as to why this assert is failing since the Mongo shell demonstrates that there are 200 documents in the collection.

Also, I've tried to drop the entire database using this post, but still the assert fails.


Solution

  • Try to change mongo write concern:

    collection.setWriteConcern(WriteConcern.FsyncSafe)