Sinon-带有回调的存根函数-导致测试方法超时

Sinon - stubbing function with callback - causing test method to timeout

本文关键字:测试方法 超时 函数 存根 回调 Sinon-      更新时间:2023-12-23

我在快速路由上有一个方法,看起来像这样:

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的好文档(这可能对测试错误场景很有用)