koa api测试文档

2020-05-21  本文已影响0人  拉面的无聊时光

api测试重点

image.png

测试执行阶段

image.png

api接口测试时,需要在构建环境启动服务并且连接mock数据库,mock数据库要通过migration来和其他环境数据库字段格式保持一致,通过seed文件向mock数据库传入基础数据

相关工具库

代码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 效果图


image.png
上一篇 下一篇

猜你喜欢

热点阅读