2014-09-02 54 views

回答

1

这取决于你如何实现你的会话。

在我的帆的应用程序,在身份验证之后,我设置req.session.authenticated = true,与曲奇等一件事,如果你正在做类似的事情是在你的/login路线,你可以做的,添加:

if (process.env.NODE_ENV === 'test') { 
    req.session.authenticated = true; 
    // do what you would do next after authentication 
} else { 
    // do normal login procedures 
} 

然后在您的测试,在before钩,你可以用superagent作出的/login路线的请求进行身份验证:

describe('MyController', function() { 
    var agent; 

    before(function (done) { 
    agent = require('superagent').agent('YOUR_APP_URL'); 

    // authenticate 
    agent 
     .post('/login') 
     .end(done) 
    }); 

    // in your tests, use the same agent to make future requests 
    describe('#someAction', function() { 
    it('should do something', function(done) { 
     agent. 
     .post('someAction') 
     .end(function (err, res) { 
      // should work! 
     }); 
    }); 
    }); 
}); 

这只是一个想法 - 你能适应这种方法不过你checki ng会话。这适用于使用Mocha进行测试的我的Sails应用程序。