摩卡咖啡,chai测试nodejs中的帖子

时间:2018-12-18 08:01:22

标签: node.js mocha sequelize.js chai

我是使用sequelize.js在node.js中进行单元测试的新手。我遵循了this tutorial to implement the api.,它工作正常,我需要对其进行测试以进行单元测试。 首先,我尝试测试User类的发布操作。以下代码将使用Mocha进行测试。

// create a user
app.post('/api/users', (req, res) => {
User.create(req.body)
    .then(user => res.json(user))
})

我的尝试如下。

var index = require('../index');
describe('POST testing on user', function () {
 it('should add a row', function (done) {
    index.post('/api/users').send({ 'name': 'he' })
        .end(function (err, res) {
            chai.expect(res.status).to.equal(200);
            done();
        })
 });
});

然后它将给出此错误,指出index.post不是函数。我应该在哪里弄错,以及如何纠正它以执行测试用例。

1 个答案:

答案 0 :(得分:0)

您的逻辑是完全错误的。应该是:

describe('/POST Create User', () => {
    it('Create User Testing', (done) => {
        let user = {
            'name': 'he'
        }
        chai.request('http://localhost:3000')
            .post('/api/users')
            .send(user)
            .end((err, res) => {

            });
        });
    });
});