AssertionError: expected undefined to equal 201

AssertionError: expected undefined to equal 201

我有一个 post 用户 API(数据库是 Postgres),它在 postman 上运行良好,没有错误,我的 [=] 中有 bodyparser 13=], .type('json') 但我的测试返回 AssertionError: expected undefined to equal 201, I console.log(res), 这是未定义的。这是我的代码,我的 API 在前,用 mocha 和 chai 测试在后。

exports.post_user = (req, res) => {
    const {error} = validateUser(req.body);
    if (error) return res.status(422).json({ message: error.details[0].message });
    if (!req.file) return res.send('Please upload a file');
    bcrypt.hash(req.body.password, 10, async (err, hash) => {
        if (err) {
            res.status(500).json({
                message: 'retype password',
                error: err
            });
        } else {
            const text = `INSERT INTO 
    users(id, firstName, lastName, otherName, email, phoneNumber, userName, isAdmin, password, userImage, createdOn) 
    VALUES(, , , , , , , , , , ) 
    returning *`;
            const values = [
                uuidv4(),
                req.body.firstName,
                req.body.lastName,
                req.body.otherName,
                req.body.email,
                req.body.phoneNumber,
                req.body.userName,
                req.body.isAdmin,
                hash,
                req.file.path,
                moment(new Date())
            ];
            try {
                const { rows } = await db.query(text, values);
                const token = jwt.sign({
                    email: rows[0].email,
                    userId: rows[0].id
                }, process.env.SECRET,
                {
                    expiresIn: '24h'
                });
                return res.status(201).json({
                    message: 'user created successfully',
                    users: rows[0],
                    token: token,
                    request: {
                        type: 'GET',
                        url: 'http://localhost:3000/api/v1/user/' + rows[0].id
                    }
                });
            } catch (err) {
                return res.status(400).json({
                    message: 'an error occur',
                    error: console.error(err)
                });
            }
        }
    });


};
process.env.NODE_ENV === 'test';

import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../../app';
import faker from 'faker';
import moment from 'moment';

const { expect } = chai;
// using chai-http middleware
chai.use(chaiHttp);

describe('POST USER', () => {
    it('Should successfully create a user account if inputs are valid', (done) => {
        chai.request(app)
            .post('/api/v1/user/signup')
            .type('json')
            .send({
                userImage: faker.image.people(),
                firstName: 'Kazeem',
                lastName: 'Odutola',
                otherName: 'Oluwatobi',
                email: 'tester@gmail.com',
                password: 'Kazeem27',
                userName: 'Kaz',
                phoneNumber: '080874568356',
                isAdmin: 'yes',
                createdOn: moment(new Date())
            })
            .then((res) => {
                // eslint-disable-next-line no-console
                console.log(res.body.data);
                const { body } = res;
                expect(body).to.be.an('object');
                expect(body.status).to.be.equals(201);
                expect(body.data).to.be.an('object');
                expect(body.data.token).to.be.a('string');
                done();
            })
            .catch((error) => done(error));
    });
});

expect (body.status).to.be.equals(201) 行与您的控制器 returns 不符。您的响应正文没有状态。尝试 res.should.have.status(201)expect (res.status).to.equal(201)

带有代码 (201) 的响应是一个特殊的响应,因为它不允许任何响应主体,如果您需要一个主体,您可以使用 200 (as a successful action)