Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js mocha/supertest是否为每个测试套件创建express server?_Node.js_Express_Mocha.js_Supertest - Fatal编程技术网

Node.js mocha/supertest是否为每个测试套件创建express server?

Node.js mocha/supertest是否为每个测试套件创建express server?,node.js,express,mocha.js,supertest,Node.js,Express,Mocha.js,Supertest,从最近几天开始,我一直在使用摩卡咖啡、超级测试咖啡和proxyquire咖啡我可以毫无问题地进行集成测试。但我有一些问题。 这是我项目中的一个测试套件。 const expect = require('chai').expect const request = require('supertest') const _ = require('lodash') const sinon = require('sinon') const faker = require('faker') descr

从最近几天开始,我一直在使用摩卡咖啡、超级测试咖啡和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 app实例。


那么,supertest对那个express应用程序做了什么

它是否每次都创建新服务器以发出请求?


…如果是这样的话,是否可以为每个测试套件只创建一个express server?

是的,每次您将一个express应用程序传递给supertest时,它都会为您运行一个express server,如果您想创建一个express server并在某些单元测试中使用它,您可以在“创建服务器并多次使用之前”部分中执行此操作。 除此之外,我建议您检查一下这个模块,它非常简单,有一些很好的测试RESTAPI的特性

描述('ComboController/api/v1/combos',函数(){
让服务器;
常量app=require(“../src/app”)
之前(()=>{
服务器=请求(应用程序);
});
它('应该获取给定id的组合:getComboById',async()=>{
const response=等待服务器;
.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.key('status','message','data'))
expect(body.status).to.be.a('Boolean')。true
expect(body.data).to.be.a('对象')
})

})
如果它在侦听的端口上创建服务器?每次它都在临时端口上创建服务器并侦听该端口。顺便说一下,您的问题在supertest github页面中,我建议您访问该问题。