lundi 22 janvier 2018

Mocha and sinon - how test mongoose in function

I learning how to test node and have a simple problem with this function:

exports.create_user = function(req, res) {
    var user = new User(req.body);
    user.save(function(err) {
        res.send('ok');
    });
};

How can stub "new User" and check which params are passed to the constructor? Something like this not working:

it("saves user", function(){
  global.User = class User{
    constructor(args){
      this.args = args;
    }
    save(){

    }
  }
  sinon.stub(User.prototype, 'save');
  var constructor = sinon.spy(global, 'User');
  UsersController.create_user(this.req, this.res);
  expect(constructor.calledOnce).to.equal(true);
  //console.log(userStub.getCall(0).args);
  //expect(this.res.json.calledOnce).to.equal(true);
});

Aucun commentaire:

Enregistrer un commentaire