简体   繁体   中英

Async api request with db request in js mocha?

I build nodejs server, and now I'm testing it with mocha.

I have problem with async requests. I send my object to API, and then check record for with object in DB. I need use only co library and generators. There's error:

  TypeError: Cannot read property 'id' of null

It depends on insertUser object is null, but I don't know why object from database is null.

API works fine, and sequilize works fine.

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);
        });
    });

I solve my problem. Code is fine, but password in db is hashing and I check hash password and order password

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM