我现在正试图测试我的控制器,需要访问session。我发现可以使用superagent进行登录,但我的唯一选项是通过google oauth登录Web应用程序,目前我找不到任何适用于Mocha测试的合适示例。有什么帮助吗?
我现在正试图测试我的控制器,需要访问session。我发现可以使用superagent进行登录,但我的唯一选项是通过google oauth登录Web应用程序,目前我找不到任何适用于Mocha测试的合适示例。有什么帮助吗?
req.session.authenticated = true
,以及cookie等。如果您正在执行类似的操作,则可以在您的/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!
});
});
});
});
这只是一个想法 - 你可以根据自己的会话检查方式进行调整。这适用于我使用 Mocha 进行测试的 Sails 应用程序。