[英]Using Mocha Chai with Stormpath
我的 google-fu 可能让我失败了,但我正在研究为使用 Stormpath 进行身份验证的 Express 应用程序构建单元测试。 所以一条典型的路线看起来像这样;
app.get('/api/user', stormpath.loginRequired, userApi.get);
在这种情况下,我将如何使用像 Mocha + Chai 这样的测试框架?
谢谢@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();
});
});
看看我在 express-stormpath 库中编写的集成测试,例如: https : //github.com/stormpath/express-stormpath/blob/master/test/middlewares/test-groups-required。 js#L208
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.