【问题标题】:mocha, chai testing for post in nodejsmocha, chai 测试 nodejs 中的帖子
【发布时间】:2018-12-18 08:01:21
【问题描述】:

我是使用 sequelize.js 在 node.js 中进行单元测试的新手。我关注了this tutorial to implement the api. 它工作正常,我需要对其进行测试以进行单元测试。 首先,我尝试测试 User 类的 post 操作。以下代码将使用 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 不是一个函数。我应该在哪里出错以及如何纠正它以执行测试用例。

【问题讨论】:

    标签: node.js mocha.js sequelize.js chai


    【解决方案1】:

    你的逻辑大错特错。应该是:

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

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2020-12-07
      • 1970-01-01
      • 2020-08-18
      • 2017-10-21
      相关资源
      最近更新 更多