简体   繁体   English

如何在Node.js中的CompositeJS应用程序中模拟会话对象

[英]How to mock session Object in compoundjs application in node.js

I have a compound js application in node.js. 我在node.js中有一个复合js应用程序。 For some actions i have a filter checking the existence of "session.user" value which gets populated only after successful authentication. 对于某些操作,我有一个过滤器检查“ session.user”值的存在,该值只有在成功通过身份验证后才会填充。 But whenever writing the unit test cases for such actions, the session.user value gets undefined. 但是,每当为此类操作编写单元测试用例时,session.user值都将变得不确定。 I use supertest, sinon modules to make get,post requests to actions but due to undefined value of "session.user" value, it doesn't authenticate and gets redirected to proper action. 我使用超级测试,sinon模块来获取对操作的获取,发布请求,但是由于未定义“ session.user”值的值,因此无法进行身份验证,而是将其重定向到正确的操作。 Please help me out as I am stuck with the same. 请帮我,因为我一直坚持下去。

Please guide me what exactly I need to mock so that I can authenticate the session.user value. 请指导我到底需要模拟什么,以便可以验证session.user值。 Thanks in advance, 提前致谢,

waiting for positive answers, 等待肯定的答案,

Thanks, 谢谢,

I used a combination of mocking passport.initialize, a test helper, and a listener on the compound configure event. 我在复合configure事件中结合使用了嘲笑passport.initialize,测试帮助程序和侦听器的组合。

This provided two things: 这提供了两件事:

  1. DRY - reuse of beforeEach code across controller tests. beforeEach在控制器测试之间重用beforeEach代码。
  2. Unobtrusive testing - mock passport.initialize so I didn't have to modify configuration based on testing. 不引人注目的测试-模拟护照.initialize,因此我不必根据测试修改配置。

In test/init.js I added the method to mock passport.initialize**: 在test / init.js中,我添加了方法来模拟passport.initialize **:

** Found it at: **在以下位置找到它:

http://hackerpreneurialism.com/post/48344246498/node-js-testing-mocking-authenticated-passport-js http://hackerpreneurialism.com/post/48344246498/node-js-testing-mocking-authenticated-passport-js

// Fake user login with passport.
app.mockPassportInitialize = function () {
    var passport = require('passport');
    passport.initialize = function () {
        return function (req, res, next) {
            passport = this;
            passport._key = 'passport';
            passport._userProperty = 'user';
            passport.serializeUser = function(user, done) {
                return done(null, user.id);
            };
            passport.deserializeUser = function(user, done) {
                return done(null, user);
            };
            req._passport = {
                instance: passport
            };
            req._passport.session = {
                user: new app.models.User({ id: 1, name: 'Joe Rogan' })
            };

            return next();
        };
    };
};

Then I added a helpers.js file to be called in each controller: 然后,我添加了一个helpers.js文件,以在每个控制器中调用该文件:

module.exports = {
    prepApp: function (done) {
        var app = getApp();
        compound = app.compound;
        compound.on('configure', function () { app.mockPassportInitialize(); });
        compound.on('ready', function () { done(); });
        return app;
    }
};

This will be called in the beforeEach of each controller: 这将在每个控制器的beforeEach中调用:

describe('UserController', function () {
    beforeEach(function (done) {
        app = require('../helpers.js').prepApp(done);
    });
    [...]
});

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

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