我正在使用 mocha、chai 和 chai-http 对我的 Node.js 和 Express 应用程序进行集成测试。我正在尝试测试我的登录路线是否有令牌。我尝试了不同的断言来检查数据对象内部是否存在令牌,但它们都不起作用。
来自 Postman 的数据如下所示:
{
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NiwiaWF0IjoxNTYzODczMzE3LCJleHAiOjE1NjM5NTk3MTd9.bM1RFem2pnlEqjTWGhg-s4Am1PjGNRUS_8x5Dq8J6lI",
"user": {
"id": 6,
"firstName": "John",
"lastName": "Doe",
"email": "test@test.com",
"createdAt": "2019-07-23T06:38:11.358Z",
"updatedAt": "2019-07-23T06:38:11.358Z"
}
},
"hasErrors": false,
"errors": []
}
这是我的实际测试,我试图检查 res.body 是否在数据对象中嵌套了令牌。
/* Login Test */
let credentials = {
'email': 'adnan@test.com',
'password': 'test'
};
describe("POST /user-sessions", function() {
it("should login user", function(done) {
chai.request('http://localhost:3000')
.post("/user-sessions")
.send(credentials)
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.nested.property('data.token');
done();
})
});
})
我收到 Uncaught AssertionError: expected { Object (data, hasErrors, ...) } to have property 'data.token'