简体   繁体   English

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

[英]Sinon - stubbing function with callback - causing test method to timeout

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('welcome', {}, function(err) {
            res.render('account/register', {
                title: 'Register'
            });
        });
    });
};

I've got a test, where I have email stubbed. 我有一个测试,我有email存根。

email is a module I require in the route. email是我在路线中require的模块。
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 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();
            });
    });
});

When I run the test, both fail, citing: 当我运行测试时,都失败了,引用:

1) Controller.Account POST /account/register creates account: Error: timeout of 2000ms exceeded at null. 1)Controller.Account POST / account / register创建帐户:错误:超过2000ms超时为空。 (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14)在Timer.listOnTimeout [as onoutout](timers.js:110:15)

2) Controller.Account POST /account/register sends welcome email: Error: timeout of 2000ms exceeded at null. 2)Controller.Account POST / account / register发送欢迎电子邮件:错误:超过2000ms超时为空。 (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) (/usr/local/lib/node_modules/mocha/lib/runnable.js:165:14)在Timer.listOnTimeout [as onoutout](timers.js:110:15)

If I comment out email.sendOne('welcome', {}, function(err) { in my route, then the first test (create account) passes. 如果我注释掉email.sendOne('welcome', {}, function(err) {在我的路线中,则第一个测试(创建帐户)通过。

Have I missed something when setting up my sinon stub? 在设置我的sinon存根时我是否错过了什么?

Sinon stubs will not automatically fire any callback functions, you need to do this manually. Sinon存根不会自动触发任何回调函数,您需要手动执行此操作。 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 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();
            });
    });
});

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) Sinon Stubs API在callsArg上有一些很好的文档,还有一些callArgWith(这对你测试错误场景很有帮助)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM