将 Mocha Chai 与 Stormpath 结合使用
Using Mocha Chai with Stormpath
我的 google-fu 可能让我失望了,但我正在研究为使用 Stormpath 进行身份验证的 Express 应用程序构建单元测试。所以典型的路线看起来像这样;
app.get('/api/user', stormpath.loginRequired, userApi.get);
在这种情况下,我将如何使用像 Mocha + Chai 这样的测试框架?
看看我在 express-stormpath 库中编写的集成测试,例如:https://github.com/stormpath/express-stormpath/blob/master/test/middlewares/test-groups-required.js#L208
感谢@rdegges,这让我找到了一个好的解决方案。我可以看到如何在 stormpath 中创建一个测试用户帐户,但就我而言,我很高兴已经在 Stormpath 中设置了一个测试用户。所以我只需要创建会话,然后进行实际测试 运行(所以我只需要 Chai 来保持会话)。所以这是我最终得到的解决方案。
var agent = chai.request.agent(server);
agent
.post('/login')
.set('Accept', 'application/json')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ login: '<username>', password: '<password>' })
.then(function (res) {
// The `agent` now has the session cookie saved, and will send it
// back to the server in the next request:
agent
.put('/user')
.send({user:data})
.end((err, res) => {
if (err){
Logger.error(res.text);
}
res.should.have.status(200);
// Do testing here
done();
});
});
我的 google-fu 可能让我失望了,但我正在研究为使用 Stormpath 进行身份验证的 Express 应用程序构建单元测试。所以典型的路线看起来像这样;
app.get('/api/user', stormpath.loginRequired, userApi.get);
在这种情况下,我将如何使用像 Mocha + Chai 这样的测试框架?
看看我在 express-stormpath 库中编写的集成测试,例如:https://github.com/stormpath/express-stormpath/blob/master/test/middlewares/test-groups-required.js#L208
感谢@rdegges,这让我找到了一个好的解决方案。我可以看到如何在 stormpath 中创建一个测试用户帐户,但就我而言,我很高兴已经在 Stormpath 中设置了一个测试用户。所以我只需要创建会话,然后进行实际测试 运行(所以我只需要 Chai 来保持会话)。所以这是我最终得到的解决方案。
var agent = chai.request.agent(server);
agent
.post('/login')
.set('Accept', 'application/json')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ login: '<username>', password: '<password>' })
.then(function (res) {
// The `agent` now has the session cookie saved, and will send it
// back to the server in the next request:
agent
.put('/user')
.send({user:data})
.end((err, res) => {
if (err){
Logger.error(res.text);
}
res.should.have.status(200);
// Do testing here
done();
});
});