NodeJS对会话对象的超级测试访问 [英] NodeJS supertest access to session object

查看:54
本文介绍了NodeJS对会话对象的超级测试访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用supertest测试我的Node.js应用程序.在我的控制器中,我访问会话对象.为了发出有效请求,该会话对象需要填充一些数据.

I'm testing my Node.js application with supertest. In my controller I access the session object. In order to make a valid request this session object needs to be filled with some data.

控制器

        // determine whether it is user's own profile or not
        var ownProfile = userId == req.session.user._id ? true : false;

测试

it('profile', function (done) {
    testUserOne.save(function(error, user){

        request
            .agent(server)
            .get('/profile?userId=' + user._id)
            .expect('Content-Type', /html/)
            .expect(200)
            .expect(/Profile/)
            .end(done);
    })
});

问题

如何模拟req/session对象?

How can I mock the req/session object?

推荐答案

仅用作子应用,然后在父应用中调用您的authenticated():

just use as sub-app, and call your authenticated() at parent-app:

var mockApp = express();
mockApp.use(session);
mockApp.all('*', function(req, res, next) {
    //authenticated(req, res, next);
    //OR
    req.session.uid = 'mock uid';
    next();
});
mockApp.use(app);

您的所有路线都会在匹配之前经过验证!

all your routes will be authenticated before matched!

这篇关于NodeJS对会话对象的超级测试访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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