The test (below) for this piece of code is failing:
module.exports = function(User, jwt) {
'use strict';
return function(req, res) {
User.create(req.body)
.then(function(id) {
var token = jwt.sign({id: id});
res.json({token: token});
});
};
};
Here is the test:
'use strict';
var chai = require('chai');
var sinon = require('sinon');
require('sinon-as-promised');
chai.should();
chai.use(require('sinon-chai'));
describe('routes/signup', function() {
var User;
var request;
var response;
var jwt;
var signup;
beforeEach(function() {
User = {create: sinon.stub()};
request = {body: 'body'};
response = {json: sinon.spy()};
jwt = {sign: sinon.stub().withArgs({id:'id'}).returns('token')};
signup = require('../../../routes/signup')(User, jwt);
});
it('returns token when resolving', function() {
User.create.resolves('id');
signup(request, response);
return response.json.should.have.been.calledWith({token: 'token'});
});
});
If I interrogate the response.json method it appears to have never been called. response.args is an empty array.
I'm guessing I'm missing something fundamental with testing promises. Help!
Remember promises are async, so you need to wait until the promise resolves and then call
then
method to do your assertions. fr0609's answer will give you a false positive because it will return before thethen
block is ever called.Try this: