[英]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.