自最近几天以来,我一直在使用mocha
,supertest
和proxyquire
。我可以毫无问题地进行集成测试。但是我有一些疑问。这是我项目中的一个测试套件。
const expect = require('chai').expect
const request = require('supertest')
const _ = require('lodash')
const sinon = require('sinon')
const faker = require('faker')
describe('ComboController /api/v1/combos', function () {
const app = require('../src/app')
it('should GET combo of given id: getComboById', async () => {
const response = await request(app)
.get(`/api/v1/combos/${faker.random.alphaNumeric(1)}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
const body = response.body
expect(body).to.have.keys('status', 'message', 'data')
expect(body.status).to.be.a('Boolean').true
expect(body.data).to.be.a('Object')
})
})
所以我想在这里知道。摩卡咖啡在这里的作用是什么?我知道可以使用supertest
发出http请求。但是对于每个测试套件,我都会传递一个Express应用程序实例。因此,该Express应用程序的超级测试是什么?是否每次都创建新服务器来发出请求?..and如果可以,是否可以为每个测试套件仅创建一个快递服务器?
是的,每次通过Express应用进行超级测试时,它都会为您运行一个Express服务器,如果您想创建Express服务器并在某些单元测试中使用它,则可以在before部分中创建服务器。并多次使用。除此之外,我建议您检查rest-bdd-testing模块,它非常简单,具有一些用于测试REST API的出色功能。
describe('ComboController /api/v1/combos', function () {
let server;
const app = require('../src/app')
before(()=> {
server = request(app);
});
it('should GET combo of given id: getComboById', async () => {
const response = await server;
.get(`/api/v1/combos/${faker.random.alphaNumeric(1)}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
const body = response.body
expect(body).to.have.keys('status', 'message', 'data')
expect(body.status).to.be.a('Boolean').true
expect(body.data).to.be.a('Object')
})
})