Sinon - 带回调的存根函数 - 导致测试方法超时 [英] Sinon - stubbing function with callback - causing test method to timeout

查看:141
本文介绍了Sinon - 带回调的存根函数 - 导致测试方法超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在快递路线上有一个方法如下:

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'
            });
        });
    });
};

我有一个测试,我有电子邮件 stubbed。

I've got a test, where I have email stubbed.

电子邮件是一个模块我 require 在路线中。

它有如下函数:

email is a module I require in the route.
It has a function like:

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();
            });
    });
});

当我运行测试时,两者都失败,引用:

When I run the test, both fail, citing:


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)

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发送欢迎电子邮件:
错误:2000ms的超时时间超过
。 (/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 welcome 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)

如果我注释掉 email.sendOne('welcome',{},function(err){ in我的路线,然后第一个测试(创建帐户)通过。

If I comment out email.sendOne('welcome', {}, function(err) { in my route, then the first test (create account) passes.

我在设置我的sinon存根时错过了什么?

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. 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();
            });
    });
});

请注意具体行:

        email.sendOne.callsArg(2);

Sinon Stubs API 有一些关于callsArg的良好文档,还有callArgWith(这可能对你测试错误场景很有帮助)

The Sinon Stubs API has some good documentation on callsArg, and also callsArgWith (which may be useful for you testing error scenarios)

这篇关于Sinon - 带回调的存根函数 - 导致测试方法超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆