mocha,chai 在 nodejs 中测试 post
mocha, chai testing for post in nodejs
我是 node.js 和 sequelize.js 的单元测试新手。我关注 this tutorial to implement the api. 它工作正常,我需要对其进行单元测试。
首先,我尝试为用户 class 测试 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 不是一个函数。我应该在哪里出错,我该如何纠正才能执行测试用例。
你的逻辑完全错误。应该是:
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) => {
});
});
});
});
我是 node.js 和 sequelize.js 的单元测试新手。我关注 this tutorial to implement the api. 它工作正常,我需要对其进行单元测试。 首先,我尝试为用户 class 测试 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 不是一个函数。我应该在哪里出错,我该如何纠正才能执行测试用例。
你的逻辑完全错误。应该是:
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) => {
});
});
});
});