开发者

Async Testing with Vows and Mongoose

Using Vows/Coffeescript/Mongoose and running into an async issue with DB.

Before my tests run I do a bunch of set up, including purging the test database. Once I have called remove on the last doc, I fire off the callback so Vows can carry on to the next step in the test. The trouble is that there's no guarantee the DB will actually have been purged since things happen async. In this case I actually WANT sync but I'm not sure how to make that happen.

Here's the code (Vows snipp开发者_StackOverflow社区et and then the purge function def):

'AND the test database is empty': 
  topic: ->
    testDB.purge "widgets_test", @callback

purge = (database, callback) ->
db = mongoose.connect "mongodb://localhost/#{database}"
modelCount = Models.length
for model in Models
    modelCount--
    model.find {}, (err, docs) ->
        docCount = docs.length
        for doc in docs
            doc.remove (err) ->
            docCount--
    # do callback after all data has been purged - setTimeout HACKHACKHACK
        if modelCount is 0 and docCount is 0
            setTimeout -> 
                callback()
            , 100

What's the right way to do this?


The trick I use is to do this setups/teardowns in separate Batchs before you run your tests in a subsequent Batch.

It seems to work for me (I'm using also vows and Mongo)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜