摩卡它()testing只通过,如果没有事先testing

我正在testing一个MEAN应用程序。 每个testing只是确保我得到正确的回应代码。 在我的路由器中,他们使用res.send()成功返回json。

这里给出的第二个testing只有当第一个被注释掉时才会通过。 如果我把第一个testing评论出来,第一个testing通过,但第二个testing通过。

这种行为对于这两个testing不是唯一的。 在“接受经济长期”之前,还有另外一个考验。 如果我评论一下,接受经济和长期的工作。 如果我离开它,接受经纬度。 我需要做些什么才能让这些asynchronoustesting通过?

我曾尝试将超时设置为60秒,但这也不起作用。

var assert = require('assert'); var server = require('../bin/www'); var request = require('supertest'); request = request('http://localhost:3000'); describe('POST service request', function(){ this.timeout(5000); var postRequest = request.post('/requests.json').type('form'); ... (other commented out tests) ... // it('accepts lat and long', function (done){ // postRequest.send({ // service_code: 2000, // long: 400, // lat: 3003 // }).expect(200, done); // }); it('accepts an address id only', function (done){ console.log('22222') postRequest.send({ service_code: 100, address_id: 400 }).expect(200, done); }); }); 

下面是一些logging输出,当他们都没有注释:

 Listening on port 3000 POST service request ===REQUEST STARTED=== Trying to save a request. DB connected. Saved. POST /requests.json 200 40.368 ms - 49 ✓ accepts lat and long (47ms) 22222 1) accepts an address id only 1 passing (5s) 1 failing 1) POST service request accepts an address id only: Error: timeout of 5000ms exceeded at null.<anonymous> (/usr/lib/node_modules/mocha/lib/runnable.js:159:19) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) npm ERR! Test failed. See above for more details. npm ERR! not ok code 0 

然后它超时。

您不能在testing之间重复使用相同的postRequest,因为send请求具有副作用。 在每个testing中创build一个新的请求:

 it('accepts lat and long', function (done){ request.post('/requests.json').type('form').send({ service_code: 2000, long: 400, lat: 3003 }).expect(200, done); }); it('accepts an address id only', function (done){ request.post('/requests.json').type('form').send({ service_code: 100, address_id: 400 }).expect(200, done); });