Frage

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.

War es hilfreich?

Lösung

Try to change mongo write concern:

collection.setWriteConcern(WriteConcern.FsyncSafe)

Andere Tipps

There are a few options:

One to change mongo write concern, as well pointed out by Sergey.

Mongo writes assynchronously by default, this means that when you fire your insert, it will not wait for the data to be inserted and move on. Changing the write concern will make your test work, but may mask problems if you are not using this option in production environment, depending on what you are testing.

The other option would be to put a wait before doing your assert, which could be trickier.

And last you could use getLastError, which will block your execution until the last command is executed.

Read more here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top