Zec*_*eck 6 mocha.js node.js express supertest passport.js
我正在使用Passport.js进行身份验证(Facebook策略)并使用Mocha和Supertest进行测试.如何使用Supertest for Facebook策略创建会话并进行经过身份验证的请求?
以下是用户未登录时的示例测试:
  describe 'when user not logged in', ->
    describe 'POST /api/posts', ->
      it 'respond with 401', (done)->
        request(app).
          post(API.url('posts')).
          set('Accept', 'application/json').
          send(post: data).
          expect('Content-Type', /json/).
          expect(401, done)
谢谢你的建议:D
eri*_*sch 12
这里几乎没有什么不同的东西,所以我把我的答案分成了两部分.
1)您首先必须通过Facebook创建测试用户.您可以通过以下两种方法之一来实现:1)Facebook的Graph API,或2)通过应用程序的角色页面.
2)使用SuperTest持久化会话的推荐方法是使用名为.agent()的SuperAgent方法来持久化会话.你可以用SuperAgent做任何事情,你可以使用SuperTest.有关更多信息,请参阅此Github帖子.
var supertest = require('supertest');
var app = require('../lib/your_app_location');
describe('when user not logged in', function() {
    describe('POST /api/posts', function() {
        var agent1 = supertest.agent(app);
        agent1
            .post(API.url('posts'))
            .set('Accept', 'application/json')
            .send(post: data)
            .(end(function(err, res) {
                should.not.exist(err);
                res.should.have.status(401);
                should.exist(res.headers['set-cookie']);
                done();
            }));
    });
});
VisionMedia Github上还有一些其他好的代码片段.请在这里找到它们.
| 归档时间: | 
 | 
| 查看次数: | 7433 次 | 
| 最近记录: |