api测试重点

测试执行阶段

api接口测试时,需要在构建环境启动服务并且连接mock数据库,mock数据库要通过migration来和其他环境数据库字段格式保持一致,通过seed文件向mock数据库传入基础数据
相关工具库
-
supertest 请求断言工具https://github.com/visionmedia/supertest
-
mocha 单元测试框架 https://mochajs.cn/
-
sinon mock和沙盒库https://sinonjs.org/
-
istanbul 代码覆盖率
代码demo
const sandbox = sinon.createSandbox()
describe('教师创建创建教学班', () => {
it('无创建者的uid时,返回400', async () => {
await request
.post('/rooms/teacher-room')
.expect(400)
.expect(res => {
expect(res.body.msg).to.equal('uid参数必须')
})
})
afterEach(function () {
// completely restore all fakes created through the sandbox
sandbox.restore()
})
it('创建者不是usercore用户,外部usercore接口返回{id:""}', async () => {
const uid = '8888888888'
sandbox.stub(usercore, 'get').value(async function () {
return {
status: 200,
data: { id: '' }
}
})
await request
.post('/rooms/teacher-room')
.set('uid', uid)
.send()
.expect(400)
.expect(res => {
expect(res.body.msg).to.equal('无效用户')
})
})
//...other
执行 npm run test 效果图

网友评论