2016-03-02 23 views
2

我是新来测试。最近我开发了一个Sails项目。它工作正常。但是现在我想为整个项目编写测试用例。我面临的问题是如何首先进行身份验证,然后使用Mocha和Unit Js测试Sails项目。如何使用Mocha和Unitjs来测试需要登录的Sails js控制器?

我的一个控制器有一个显示报告的功能,但只显示那些登录的用户。我该如何编写这种逻辑的测试用例?

感谢

+0

试试这个, VAR请求=需要( 'supertest'); var cookie; 请求(应用程序) .POST( '/登录') 。发送({电子邮件: “[email protected]”,密码: '密码'}) .END(功能(ERR,RES){ 水库should.have.status(200); cookie = res.headers ['set-cookie']; done(); }); // // 和使用(应用程序) 获得( '/ V1 /你/路径') .SET( '饼干',饼干) .END(功能上的下一个请求 请求的cookie(ERR ,res){res.should.have.status(200); done(); }); 我发现它感谢 –

回答

1

试试这个,

var request=require('supertest'); 
    var cookie; 
    request(app) 
    .post('/login') 
    .send({ email: "[email protected]", password:'password' }) 
    .end(function(err,res){ 
    res.should.have.status(200); 
    cookie = res.headers['set-cookie']; 
    done();   
    }); 

    // 
    // and use the cookie on the next request 
    request(app) 
    .get('/v1/your/path') 
    .set('cookie', cookie) 
    .end(function(err,res){ 
    res.should.have.status(200); 
    done();   
    }); 

感谢

1

在测试环境您可以使用Cookie来保留会话信息。您可以检查了Cookie的使用机智帆,摩卡和supertest我GIST例如: gist

相关问题