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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

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 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