0
我構建nodejs服務器,現在我正在用mocha測試它。在js mocha中使用db請求的異步api請求?
我有異步請求的問題。我將我的對象發送給API,然後檢查DB中的對象的記錄。我只需要使用co庫和生成器。 有錯誤:
TypeError: Cannot read property 'id' of null
這取決於insertUser對象是空的,但我不知道爲什麼從數據庫對象爲null。
API工作正常,並且sequilize工作正常。
it('it should POST a user',() => {
return co(function *() {
let user = {
name: "testInsertUser",
password: "12345"
};
let res = yield chai.request(server)
.post('/api/users')
.send(user);
res.should.have.status(HTTPStatus.OK);
res.body.should.be.a('object');
res.body.should.have.property('name').eql(user.name);
res.body.should.not.have.property('password');
//Find user in db
let insertUser =
yield models.User.findOne({
where: {
name: user.name,
password: user.password
}
});
res.body.should.have.property('id').eql(insertUser.id);
});
});