每次运行测试前如何登录 [英] How to login each time before running a test

查看:68
本文介绍了每次运行测试前如何登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新手,正在用Javascript测试,并尝试使用mocha和chai测试我的NodeJS后端. 我的所有路线都充满了中间件,这种中间件不允许人们在未登录的情况下继续前进.

I am a newbie in testing in Javascript and trying to test my NodeJS backend using mocha and chai. How all of my routes are filled with a middleware which does not allow people to go forward if they aren't logged in.

类似这样的东西

app.post('/user/analytics/driverdata', checkauth, function(req, res) {
        analytics.driverData(req, res);
});

其中checkauth

var checkauth = function(req, res, next) {
    console.log("In checkauth");
    if (req.isAuthenticated()) {
        next();
    } else {
        console.log("Doesn't authenticate");
        res.status(401);
        res.set('Content-Type', 'application/json');
        res.end(JSON.stringify({
            'success': false
        }));
    }
};

isAuthenticated参数在反序列化请求时由PassportJS附加到请求. 我想做的是为

The isAuthenticated parameter is attached to request by PassportJS, when it deserializes a request. What I want to do is write a test for

app.post('/user/analytics/driverdata', checkauth, function(req, res) {
            analytics.driverData(req, res);
});

此API.我无法登录,因此无法到达那里. 因此,我编写了一个beforeEach来登录用户beforeEach it.就像这样.

this API. in which I am failing as I am not logged in hence unable to reach there. So I wrote a beforeEach to login the user beforeEach it. It goes like this.

var expect = require('chai').expect;
var request = require('superagent');

beforeEach(function(done){
        //login into the system
        request
        .post("http:localhost:5223/user/authenticate/login")
        .send({username : "saras.arya@gmail.com", password : "saras"})
        .end(function assert(err, res){
        if(err){
            console.log(err);
            done();
        }
        else{
            done();
        }
    });
});

我不知道我在做什么错,互联网使我失败了.任何指出我要去哪里的帮助将不胜感激.

I don't know what am I doing wrong and the internet has failed me. Any help to point out where am I going wrong will be appreciated.

推荐答案

看到很多东西和烦躁不安之后,我认为我终于破解了它.不提此处的答案,这使我引以为傲.代理的概念.这帮助我破解了这个问题. 在您的describe块内部,或者可能在该块之前,您可以具有以下it.

After seeing a lot of stuff and fidgeting around I think I finally cracked it. It would be harsh not to mention the answer here which introduced me to the concept of an agent. Which helped me crack this. Inside your describe block, or probably before the block you can have the following it.

var superagent = require('superagent');
var agent = superagent.agent();
it('should create a user session successfully', function(done) {
       agent
       .post('http://localhost:5223/user/authenticate/login')
       .send({
              username: 'whatever@example.com',
              password: 'ssh-its-a-secret'
        })
        .end(function(err, res) {
             console.log(res.statusCode);
             if (expect(res.statusCode).to.equal(200))
                 return done();
             else {
                 return done(new Error("The login is not happening"));
                    }
                });
        });

agent变量为您保存cookie,然后PassportJS使用这些cookie对您进行身份验证.

the agent variable holds the cookies for you, which are then used by PassportJS to authenticate you.

这是您的操作方式.因此,agent变量位于describe内部.在同一describe中的另一个it中.

here is how you do it. So the agent variable is inside a describe. In the same describe inside another it.

it("should test analytics controller", function(done) {
agent.post('http://localhost:5040/user/analytics/driverData')
        .send({
            startDate: "",
            endDate: "",
            driverId: ""
        })
        .end(function(err, res) {
            if(!err)
            done();
        });
});

此功能像魅力一样传递.这是缺少的完整文档.

This function passes like a charm. This was one complete documentation that was missing.

这篇关于每次运行测试前如何登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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