簡體   English   中英

摩卡咖啡,chai測試nodejs中的帖子

[英]mocha, chai testing for post in nodejs

我是使用sequelize.js在node.js中進行單元測試的新手。 我按照本教程實施了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不是一個函數。 我應該在哪里弄錯,以及如何糾正它以執行測試用例。

您的邏輯是完全錯誤的。 它應該是:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM