Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Mocha.js: how to test consequent save and delete with mongoose

Mocha.js: how to test consequent save and delete with mongoose

Problem

Testing framework: mocha I want to test saving and then deleting created document in mongodb (with mongoose)

Code that actually do this :

item = new Item()
item.save(function(err, data){
   // if no errors test passed, then I need to test removing that item 
   Item.remove({_id: data.id})
})

How should I describe the test?

I want to have separate tests results for save and remove in output.

Thanks.

Problem courtesy of: WHITECOLOR

Solution

var User = require('../../models/user');

describe('User', function(){
  describe('#save()', function(){
    it('should save without error', function(done){
      var user = new User({        username    : 'Luna'
        , email     : '[email protected]'
        , password  : 'asdf123'
      });
      user.save(function(err){
        if (err) throw err;
        done();
      });
    })
  })
})
Solution courtesy of: chovy

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Mocha.js: how to test consequent save and delete with mongoose

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×