Sinon-带有回调的存根函数-导致测试方法超时
Sinon - stubbing function with callback - causing test method to timeout
我在快速路由上有一个方法,看起来像这样:
exports.register_post = function(req, res) {
var account = new Account();
account.firstName = req.param('firstName');
//etc...
account.save(function(err, result) {
email.sendOne('welcome', {}, function(err) {
res.render('account/register', {
title: 'Register'
});
});
});
};
我有一个测试,在那里我有email
存根。
CCD_ 2是路由中的模块I CCD_
它的功能类似于:
exports = module.exports.sendOne = function(templateName, locals, cb)
我的测试是这样的:
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 welcome 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, 'welcome');
done();
});
});
});
当我运行测试时,两者都失败了,引用:
1) 控制器。帐户POST/Account/register创建帐户:错误:超时超过2000ms为null。(/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14)在Timer.listOnTimeout〔as ontimeout〕(timers.js:11:15)
2) 控制器。帐户POST/帐户/注册发送欢迎电子邮件:错误:超时超过2000ms为null。(/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14)在Timer.listOnTimeout〔as ontimeout〕(timers.js:11:15)
如果我在路由中注释掉email.sendOne('welcome', {}, function(err) {
,那么第一个测试(创建帐户)就通过了。
我在设置sinon存根时遗漏了什么吗?
Sinon存根不会自动触发任何回调函数,您需要手动执行。事实上,这真的很东方:
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 welcome 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, 'welcome');
done();
});
});
});
注意特定行:
email.sendOne.callsArg(2);
Sinon Stubs API有一些关于callsArg和callsArgWith的好文档(这可能对测试错误场景很有用)
相关文章:
- Sinon-带有回调的存根函数-导致测试方法超时
- AngularJS列出了所有单元测试方法
- .done 或 .then 中的茉莉花测试方法
- 如何在 JavaScript 中测试方法而不会出错
- 渲染流星模板时执行的茉莉花测试方法
- 如果“beforeEach”中的代码是异步的,如何在 Jasmine 中测试方法
- AngularJS 茉莉花承诺测试由于超时而失败
- 返回带有mocha的promise的测试方法调用
- 单元测试——方法中的模拟方法
- Javascript Regexp测试方法的怪异行为
- ReactJS的首选测试方法
- 在长轮询时避免在Protractor中出现测试同步超时
- 在模块上使用sinon测试方法调用.出口的方法
- 正则表达式测试方法中的“g”如何交替工作
- 为什么在使用承诺和跳过代码时,Mocha测试会超时?
- angularJS$立即执行方法超时
- Javascript测试方法不工作(如预期)
- 从窗口作用域函数调用的测试方法
- 如何测试方法是否只被调用了一次,而不是第二次在茉莉
- 用摩卡测试时超时