jeudi 5 mai 2016

Unit test mongoose promises with sinon

I am trying to unit test a mongoose object that uses promises. I have written the test below and it works but it's not complete. I can't figure out how to test if the 'then' or 'catch' methods are called.

How can I use a spy to check that the 'then' method is called when I resolve the promise?

Method to test

export function create(req, res) {
  User
    .createAsync(req.body)
    .then(handleCreate(res, req.originalUrl))
    .catch(handleError(res));
}

Unit test

it('should do something', () => {
  const req = {
    body: 45,
  };

  const res = {};

  const mockRole = sandbox.mock(Role).expects('createAsync').once().withArgs(45)
    .returns(Promise.resolve());

  controller.create(req, res);
});

Aucun commentaire:

Enregistrer un commentaire