admin管理员组文章数量:1323529
I have a method on a express route that looks like this:
exports.register_post = function(req, res) {
var account = new Account();
account.firstName = req.param('firstName');
//etc...
account.save(function(err, result) {
email.sendOne('wele', {}, function(err) {
res.render('account/register', {
title: 'Register'
});
});
});
};
I've got a test, where I have email
stubbed.
email
is a module I require
in the route.
It has a function like:
exports = module.exports.sendOne = function(templateName, locals, cb)
My test looks like this:
describe('POST /account/register', function(done) {
var email;
beforeEach(function(done) {
accountToPost = {
firstName: 'Alex',
};
email = require('../../app/helpers/email');
sinon.stub(email)
done();
});
afterEach(function(done) {
email.sendOne.restore();
done();
})
it('creates account', function(done) {
request(app)
.post('/account/register')
.send(this.accountToPost)
.expect(200)
.end(function(err, res) {
should.not.exist(err)
//todo: asserts
done();
});
});
it('sends wele email', function(done) {
request(app)
.post('/account/register')
.send(this.accountToPost)
.expect(200)
.end(function(err, res) {
should.not.exist(err)
sinon.assert.calledWith(email.sendOne, 'wele');
done();
});
});
});
When I run the test, both fail, citing:
1) Controller.Account POST /account/register creates account: Error: timeout of 2000ms exceeded at null. (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
2) Controller.Account POST /account/register sends wele email: Error: timeout of 2000ms exceeded at null. (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
If I ment out email.sendOne('wele', {}, function(err) {
in my route, then the first test (create account) passes.
Have I missed something when setting up my sinon stub?
I have a method on a express route that looks like this:
exports.register_post = function(req, res) {
var account = new Account();
account.firstName = req.param('firstName');
//etc...
account.save(function(err, result) {
email.sendOne('wele', {}, function(err) {
res.render('account/register', {
title: 'Register'
});
});
});
};
I've got a test, where I have email
stubbed.
email
is a module I require
in the route.
It has a function like:
exports = module.exports.sendOne = function(templateName, locals, cb)
My test looks like this:
describe('POST /account/register', function(done) {
var email;
beforeEach(function(done) {
accountToPost = {
firstName: 'Alex',
};
email = require('../../app/helpers/email');
sinon.stub(email)
done();
});
afterEach(function(done) {
email.sendOne.restore();
done();
})
it('creates account', function(done) {
request(app)
.post('/account/register')
.send(this.accountToPost)
.expect(200)
.end(function(err, res) {
should.not.exist(err)
//todo: asserts
done();
});
});
it('sends wele email', function(done) {
request(app)
.post('/account/register')
.send(this.accountToPost)
.expect(200)
.end(function(err, res) {
should.not.exist(err)
sinon.assert.calledWith(email.sendOne, 'wele');
done();
});
});
});
When I run the test, both fail, citing:
1) Controller.Account POST /account/register creates account: Error: timeout of 2000ms exceeded at null. (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
2) Controller.Account POST /account/register sends wele email: Error: timeout of 2000ms exceeded at null. (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
If I ment out email.sendOne('wele', {}, function(err) {
in my route, then the first test (create account) passes.
Have I missed something when setting up my sinon stub?
Share Improve this question asked Nov 30, 2013 at 23:49 AlexAlex 38.5k54 gold badges214 silver badges339 bronze badges1 Answer
Reset to default 6Sinon stubs will not automatically fire any callback functions, you need to do this manually. It's actually really east to do though:
describe('POST /account/register', function(done) {
var email;
beforeEach(function(done) {
accountToPost = {
firstName: 'Alex',
};
email = require('../../app/helpers/email');
sinon.stub(email);
email.sendOne.callsArg(2);
done();
});
afterEach(function(done) {
email.sendOne.restore();
done();
})
it('creates account', function(done) {
request(app)
.post('/account/register')
.send(this.accountToPost)
.expect(200)
.end(function(err, res) {
should.not.exist(err)
//todo: asserts
done();
});
});
it('sends wele email', function(done) {
request(app)
.post('/account/register')
.send(this.accountToPost)
.expect(200)
.end(function(err, res) {
should.not.exist(err)
sinon.assert.calledWith(email.sendOne, 'wele');
done();
});
});
});
Notice the specific line:
email.sendOne.callsArg(2);
The Sinon Stubs API has some good documentation on callsArg, and also callsArgWith (which may be useful for you testing error scenarios)
本文标签: javascriptSinonstubbing function with callbackcausing test method to timeoutStack Overflow
版权声明:本文标题:javascript - Sinon - stubbing function with callback - causing test method to timeout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742129183a2422079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论