Node.js& Mocha / Should(JavaScript,ES6):代码未执行虽然看起来应该

时间:2017-07-12 15:28:17

标签: javascript node.js mocha should.js

我尝试使用mocha / should和request在nodejs项目中编写一些测试代码。

我的代码使用一些网址初始化一个数组,以便向远程服务器发送GET请求并检查响应的内容。

我的模型现在只需打印出响应,但由于某种原因流量永远不会到达那里。

请注意,我有一个for循环。在循环内部,第一个控制台日志打印出东西,但由于某种原因,该循环内的其余代码被跳过。我在调试模式中设置了断点,但是我的代码只到达循环内的第一个console.log()并且跳过了请求部分。

我也尝试使用未经证实的请求版本(流和所有)但我遇到了同样的问题 - 代码从未到达该请求行,所以当然它没有进一步打印内部任何内容。< / p>

是否与nodejs中的异步工作有关?别的什么?

我错过了什么?

&#13;
&#13;
'use strict';
const Promise = require('bluebird')
	, _ = require('underscore')
	, should = require('should')
	, r = require('request')
	, request = Promise.promisifyAll(r.defaults({jar: true}))
	, client = require('../whatever/someClient')
	, testConfig = require('../config/test-config')
;

Promise.longStackTraces();

class someFeatureTestSet {
	
	constructor() {
		//...
		this.client = client.getUser();
		//...
	}
	
	static create() { return new someFeatureTestSet(); }
	
//... some other consts and functions
	
	initURLs(someUrlParamVal) {
		return Array
		.apply(null, Array(someUrlParamVal))
		.map((x, idx) =>
			`http://example.com/whatever?c=${someUrlParamVal}`
		);
	}
	
	runTests() {
		const client = this.client;
		const someFeatureTestSet = this;
		
		describe('get stuff', () => {
			
			it('should bla', () => {
				const productsLinks = this.initURLs('123');

				for (let x in productsLinks) {
					console.log(productsLinks[x]); //gets printed, no problem
					request.getAsync({ uri: productsLinks[x] }) 
					.then(res => { //code never gets here. why?
						console.log(res); //code never gets here. why?
					})
				}

			});
			
		} );
		
	}
	
}

module.exports = someFeatureTestSet;

const createTestSet = () => someFeatureTestSet.create();

createTestSet().client().runTests();
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

运行异步测试后,您需要返回Promise或调用done()回调。由于您在循环中运行异步请求,因此需要在数组中累积请求promise并使用Promise.all()

it('should bla', () => {
  const productsLinks = this.initURLs('123');
  let requests = [];

  for (let x in productsLinks) {
    console.log(productsLinks[x]); //gets printed, no problem
    requests.push(request.getAsync({ uri: productsLinks[x] }));
  }

  return Promise.all(requests);
});

如果您想使用done(),可以执行以下操作 -

it('should bla', (done) => {
  const productsLinks = this.initURLs('123');
  let requests = [];

  for (let x in productsLinks) {
    console.log(productsLinks[x]); //gets printed, no problem
    requests.push(request.getAsync({ uri: productsLinks[x] }));
  }

  Promise
    .all(requests)
    .then(() => {
      done();
    })
});

请注意,如果超过为运行mocha测试设置的超时,则调用done()将失败。请参阅此处了解详情 - https://mochajs.org/#timeouts

答案 1 :(得分:0)

我怀疑问题在于彻底误解了Node.js的事件循环以及Mocha使用promises而不是旧的“完成”回调。

以下解决方案对我有用:

'use strict';
const Promise = require('bluebird')
	, _ = require('underscore')
	, should = require('should')
	, r = require('request')
	, request = Promise.promisifyAll(r.defaults({jar: true}))
	, client = require('../whatever/someClient')
	, testConfig = require('../config/test-config')
;

Promise.longStackTraces();

class someFeatureTestSet {
	
	constructor() {
		//...
		this.client = client.getUser();
		//...
	}
	
	static create() { return new someFeatureTestSet(); }
	
//... some other consts and functions
	
	initURLs(someUrlParamVal) {
		return Array
		.apply(null, Array(someUrlParamVal))
		.map((x, idx) =>
			`http://example.com/whatever?c=${someUrlParamVal}`
		);
	}
	
	runTests() {
		const client = this.client;
		const someFeatureTestSet = this;
		
		describe('get stuff', () => {
			
			it('should bla', () => {
				const productsLinks = this.initURLs('123');

				return Promise.map(productsLinks, productsLink => 
					return request.getAsync({uri: productsLink })
						.then(res => { console.log(res) });
				);

			});
			
		} );
		
	}
	
}

module.exports = someFeatureTestSet;

const createTestSet = () => someFeatureTestSet.create();

createTestSet().client().runTests();