????????????????? supertest ?????????????????????????????? mocha ????????????????????????? mocha ????????????????????????????
const co = require('co')
const { ObjectId } = require('mongoose').Types
const config = require('../config')
const UserModel = require('../models/user')
const app = require('../app')
const request = require('supertest')(app)
describe('User API'?? function (){
// ???????????????????
// ???????????п?????? context ??????????????
beforeEach(function (done){
co(function* (){
self.user1 = yield UserModel.create({ username: 'user1' })
self.token = jwt.sign({ _id: self.user1._id }?? config.jwtSecret?? { expiresIn: 3600 })
done()
}).catch(err => {
console.log('err: '?? err)
done()
})
})
// ????????·??? /user
it('should get user info when GET /user with token'?? function (done){
const self = this
request
.get('/user')
.set('Authorization'?? self.token)
.expect(200)
.end((err?? res) => {
res.body._id.should.equal(self.user1._id)
done()
})
})
// ??????????·??? /user
it('should return 403 when GET /user without token'?? function (done){
request
.get('/user')
.expect(403?? done)
})
// ???? /users?????????????????????????????????????????????
it('should return user list when GET /users'?? function (done){
request
.get('/users')
.expect(200)
.end((err?? res) => {
res.body.should.be.an.Array()
done()
})
})
// ???? /users/:userId ???????????????????
it('should return user info when GET /users/:userId'?? function (done){
const self = this
request
.get(`/users/${self.user1._id}`)
.expect(200)
.end((err?? res) => {
res.body._id.should.equal(self.user1._id)
done()
})
})
// ???????????????????????????????????? id
it('should return 404 when GET /users/${non-existent}'?? function (done){
request
.get(`/users/${ObjectId()}`)
.expect(404?? done)
})
// ????????μ???????????? token
it('should return user info when POST /user'?? function (done){
const username = 'test user'
request
.post('/users')
.send({ username: username })
.expect(200)
.end((err?? res) => {
res.body.username.should.equal(username)
done()
})
})
// ???????μ????????????? token ????????ж?????????
it('should return 400 when POST /user with token'?? function (done){
const username = 'test user 2'
request
.post('/users')
.set('Authorization'?? this.token)
.send({ username: username })
.expect(400?? done)
})
// ??????????????????????????? token
it('should return 200 when PUT /user with token'?? function (done){
request
.put('/user')
.set('Authorization'?? this.token)
.send({ username: 'valid username' })
.expect(200?? done)
})
// ???????????????????????? token
it('should return 400 when PUT /user without token'?? function (done){
request
.put('/user')
.send({ username: 'valid username' })
.expect(400?? done)
})
})
?????????????? Restful API ??д????????????????????????????????????????????????????????в?????Щ??????????????????????????????????????????~
??????????????????л?????????????????ж????????????????????????ж????
???????
??????????????????????????????????? should.js???????????
??????????? should.js ?????????????????и?????????д??
????value.should.xxx.yyy.zzz ???????? assert.equal(value?? expected) ?????????
???????????? should.js ???????? Object.prototype ?????????????? null ????????????????????κ??????
??????? should.js ?? null ????Ч??
??????????????? (value === null).should.equal(true) ??
$ npm test
User api
should get user info when GET /user with token
should return 403 when GET /user without token
should return user list when GET /users
should return user info when GET /users/:userId
should return 404 when GET /users/${non-existent}
should return user info when POST /user
should return 400 when POST /user with token
should return 200 when PUT /user with token
should return 400 when PUT /user without token
?????????????в???????????????д????????????????????????
??????????????????????????????Щ??????????????????????????е????????? Debug ?????????????Ч???
???????
???????????????????
?????? Node.js ??????£??????????????????? NODE_ENV=test ????????????????????????????????????????????????????????????μ??????????
????// config.js
????module.exports = {
????development: {}??
????production: {}??
????test: {}
????}
????// app.js
????const ENV = process.NODE_ENV || 'development'
????const config = require('./config')[ENV]
????// connect db by config
??????????????????
??????????????????????????÷?? npm scripts ?д????????????????????????????? npm run resetDB ???????
????????????????д????????????????ж???????? NODE_ENV ????????? production ?????μ??????
// resetDB.js
const env = process.NODE_ENV || 'development'
if (env === 'test' || env === 'development') {
// connect db and delete data
} else {
throw new Error('You can not run this script in production.')
}
// package.json
{
"scripts": {
"resetDB": "node scripts/resetDB.js"
}??
// ...
}
???????????????????????
????????????????????????????????????????????????????????????
????????????????????????????????????Щ????????????
???????????????????????????????????к????????????к??
?????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????
{
"scripts": {
"resetDB": "node scripts/resetDB.js"??
"test": "NODE_ENV=test npm run resetDB && mocha --harmony"
}??
// ...
}