2016-11-27 259 views
0

我寫了一個NodeJs客戶端,它爲各種組合生成API請求並將響應存儲在數組中。通過循環茉莉花腳本我試圖斷言他們。但是,我的測試在初始化API響應數組之前運行。到目前爲止,我嘗試了兩種方法並沒有運氣。茉莉花循環測試

方法1:

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

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

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

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

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

 
});

方法2:

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

 
var data; 
 

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

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

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

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

 
});

回答

0

作品對我來說,仔細檢查什麼在data

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

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

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

 
});
<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>