2016-07-13 13 views
3

まあまあ、私はWeb Developmentには新しく、最近はNodeJSを学び始めました。NightmareJSを使ったシンプルなYoutubeスクレーパーを書いて、好きな人、動画URLごとのタイトル名。モカを使ったNightmareJSコードのテストが失敗しました

私は単体テストを実践するためにモカで単体テストをしようとしていますが、何らかの理由で次のエラーで失敗します: "エラー:2000msを超えました。このテストで呼ばれています。

私はタイムアウト(最大15秒)を増やそうとしましたが、それは役に立たなかった、私はそれがどこかにぶら下がっていると思います。私は何が欠けていますか?私は、コードの構造と実装について建設的な批判を聞いてうれしく思います。ここで

は私のコードです:

var Nightmare = require('nightmare'); 
var expect = require('chai').expect; 
var assert = require('chai').assert; 
youtube_url = 'https://www.youtube.com/watch?v=0_oPsFTyhjY'; 

describe('test youtube video url results', function() { 
    it('should return the actual video url/title/author name/num of likes and views', function(done) { 
     var nightmare = Nightmare({ show: false, gotoTimeout: 3000 }) 
     nightmare 
      .goto(youtube_url) 
      .scrollTo(10000,0) 
      .wait('#comment-section-renderer-items') 
      .evaluate(function (youtube_url) { 
       var authorSelector = '#watch7-user-header > div > a'; 
       var titleSelector = '#eow-title'; 
       var vcountSelector = '#watch7-views-info > div.watch-view-count'; 
       var lcountSelector = '#watch8-sentiment-actions > span > span:nth-child(1) > button > span'; 

       var authorElement = document.querySelector(authorSelector); 
       var titleElement = document.querySelector(titleSelector); 
       var vcountElement = document.querySelector(vcountSelector); 
       var lcountElement = document.querySelector(lcountSelector); 

       var JSONres = {'VIDEO URL':url, 'VIDEO TITLE': titleElement.innerText,'AUTHOR NAME': authorElement.innerText, 
       'NUMBER OF VIEWS': vcountElement.innerText, 'NUMBER OF LIKES': lcountElement.innerText}; 

       return (JSONres) 
      },youtube_url) 
      .end() 
      .then(function (result) { 
       try{ 
        expect(result['VIDEO URL']).to.equal(youtube_url); 
        expect(result['VIDEO TITLE']).to.equal('The Best Mouse in the World?'); 
        expect(result['AUTHOR NAME']).to.equal('Unbox Therapy'); 
        assert.isAtLeast(result['NUMBER OF VIEWS'], 1816808, 'The number of views is at least the number of views that has been already seen'); 
        // It's possible to remove your like from the video so hypothetically many users may remove their likes thus there is no upper/lower 
        // bound on the like amount a video can have at any time except that it must be non-negative. 
        assert.isAtLeast(result['NUMBER OF LIKES'], 0, 'The number of likes is at least a non-negative number'); 
        done(); 
       } 
       catch(error){ 
        done(error); 
       } 
      }) 

    }); 
}); 

答えて

1

が予期せず[this.timeout(20000);で] 20秒にタイムアウトを上げ、テストが実際以上6秒かかったことがないのにちょうど(何らかの理由で)問題を解決し

-1

this.timeout(0);を使用します。タイムアウトを無効にする

関連する問題