for check
import { User } from '../models/no-sql/user'; function userCreate(req) { const user = new User({ username: req.username, password: req.password }); return user.save(); } app.get('/userCreate', function(req, res) { User.findOne({ username: req.username }).lean().exec((err, data) => { if(data){ userCreate(req).then(function() {
unit test
require('sinon-as-promised'); import request from 'supertest'; const user = { username: newUserName, password: 'password' }; factory.build('user', user, function(err, userDocument) { UserMock. expects('findOne').withArgs(sinon.match.any) .chain('lean') .chain('exec') .yields( null, undefined); const docMock = sinon.mock(userDocument); docMock.expects('save') .resolves([]); request(app) .post('/userCreate') .send({username: newUserName, password: 'password') .expect(200) .end((err, res) => { if(err) done(err); should.not.exist(err); should.equal(res.body.success, true); done(); }); });
the test comes to the return of user.save (), then the time runs out. It seems that I am missing something in unit tests, but the error does not occur. I use sinon, as I promised to resolve the promise, but does not seem to see it.
source share