2017-07-12 5 views
0

mocha/shouldとrequestを使用してnodejsプロジェクトにテストコードを書き込もうとしました。Node.js&Mocha/Should(JavaScript、ES6):コードは実行されませんでした。

私のコードは、GETリクエストをリモートサーバーに送信し、応答の内容を確認するために、いくつかのWebアドレスで配列を初期化します。

私の模擬テストでは、何らかの理由でフローがそこに到達しないようにするだけで、レスポンスを出力する必要があります。

私はforループがあることに注意してください。ループの内部では、最初のコンソールログには何かの理由があるが、そのループ内の残りのコードはスキップされている。私はデバッグモードでブレークポイントを入れますが、私のコードはループ内の最初のconsole.log()にしか到達せず、リクエスト部分はスキップされます。

私はリクエスト(ストリームとすべて)の未認証バージョンも使用しようとしましたが、同じ問題が発生しました。コードは決してそのリクエストラインに到達しませんでした。

nodejs内の非同期作業と関係しますか?何か?

私には何が欠けていますか?

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

 
Promise.longStackTraces(); 
 

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

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

 
\t \t \t }); 
 
\t \t \t 
 
\t \t }); 
 
\t \t 
 
\t } 
 
\t 
 
} 
 

 
module.exports = someFeatureTestSet; 
 

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

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

答えて

2

あなたは約束を返したり、非同期テストを実行した後done()コールバックを呼び出すためのいずれかが必要です。あなたは、アレイ内の要求の約束を蓄積し、あなたは次の操作を行うことができますdone()使用したい場合は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); 
}); 

を使用する必要がループ内で非同期要求を実行しているので -

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

。なお、 done()を呼び出すと、mochaテストを実行するためのタイムアウト設定を超えた場合に失敗します。詳細についてはこちらを参照してください - https://mochajs.org/#timeouts

0

私は、以前の "done"コールバックの代わりにNode.jsのイベントループとMochaの仕事を完全に誤解していると思われました。

次のソリューションは、私の仕事:

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

 
Promise.longStackTraces(); 
 

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

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

 
\t \t \t }); 
 
\t \t \t 
 
\t \t }); 
 
\t \t 
 
\t } 
 
\t 
 
} 
 

 
module.exports = someFeatureTestSet; 
 

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

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

関連する問題