茉莉花循环测试

时间:2016-11-27 12:43:51

标签: jasmine

我编写了一个NodeJs客户端,它为各种组合生成API请求,并将响应存储在一个数组中。通过循环Jasmine脚本,我试图断言它们。但是,我的测试在初始化API响应数组之前运行。到目前为止,我尝试了两种方法,但没有运气。

方法1:



var answersReq = require('../requests/answers_request');

var data = answersReq.answers(function(results) {
	return results;
})

describe("Answer API test", function() {

	function runTest(context) {
		describe("test array suite", function() {
			it("test array", function(done) {
				expect(context).not.toBeNull();
				done();
			});
		});
	}

	for (i = 0; i <= data.length; i++) {
		runTest(data[i]);
	}

});
&#13;
&#13;
&#13;

方法2:

&#13;
&#13;
var answersReq = require('../requests/answers_request');

var data;

describe("Answer API test", function() {

	beforeAll(function(done) {
		data = answersReq.answers(function(results) {
			data = results;
			done();
		})
	});

	function runTest(context) {
		describe("test array suite", function() {
			it("test array", function(done) {
				expect(context).not.toBeNull();
				done();
			});
		});
	}

	for (i = 0; i <= data.length; i++) {
		runTest(data[i]);
	}

});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

适合我,仔细查看data中的内容

/*** CODE ***/
var data = [1,1,1,1,1]
describe("Answer API test", function() {

	function runTest(context) {
		describe("test array suite", function() {
			it("test array", function(done) {
				expect(context).not.toBeNull();
				done();
			});
		});
	}

	for (i = 0; i <= data.length; i++) {
		runTest(data[i]);
	}

});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.min.js"></script>