2015-10-13 30 views
1

我正在嘗試爲我的快速應用程序編寫一些基於Vows的測試。server.close()不適用於Vow拆解

這裏的測試來源:

var vows = require('vows'); 
var assert = require('assert'); 
var startApp = require('./lib/start-app.js'); 

var suite = vows.describe('tournaments'); 

suite.addBatch({ 
    "When we setup the app": { 
     topic: function() { 
      return startApp(); 
     }, 
     teardown: function(topic) { 
      if (topic && topic.close) { 
       topic.close(); 
      } 
     }, 
     "it works": function(topic) { 
      assert.isObject(topic); 
     } 
    } 
}); 

suite.run(); 

而這裏的start-app.js

var app = require('../../app.js'); 

function start() { 
    var server = app.listen(56971, 'localhost'); 
    return server; 
} 

module.exports = start; 

app.js出口定期Express.js的應用程序,以創建express()

問題是,無論何時我運行測試,topic.close()在拆卸功能中都不起作用,並且測試成功後永遠掛起。我試過在網上搜索並添加很多很多的console.log,都無濟於事。

我在Node.js 4.2.0的Windows x64版本上,我使用的是[email protected][email protected]

任何想法如何讓我的測試停止掛?

+0

檢查結果在這裏同樣的問題,測試建立在Travis永遠運行。你找到解決方案嗎? –

+0

@FernandoPiancastelli我從來沒有找到一個合適的解決方案,但作爲一個骯髒的黑客,調用'主題'對象上的'.unref()'固定「這個。我懷疑有某個地方有bug,可能在Node core ... – strugee

回答

1

下面是我爲解決項目中的問題所做的工作:我正在貢獻一個項目:最後一批僅用於關閉服務器。

suite.addBatch({ 
    'terminate server': { 
    topic: function() { 
     server.close(this.callback); // this is a regular node require(`http`) server, reused in several batches 
    }, 
    'should be listening': function() { 
     /* This test is necessary to ensure the topic execution. 
     * A topic without tests will be not executed */ 
     assert.isTrue(true); 
    } 
    } 
}).export(module); 

在添加此測試之前,套件永遠不會結束執行。你可以在https://travis-ci.org/fmalk/node-static/builds/90381188

+0

很好,但它不能解決我的問題,因爲服務器被用作主題。所以在批次之外沒有提及它。 – strugee

+0

這似乎與一個開放的錯誤[鏈接](https://github.com/vowsjs/vows/issues/229)有關,如果你的'topic()'不調用'this,則不會調用teardown。 callback'。巧合的是,我的代碼調用它。 如果我正確地調用節點VM,如果您在另一批次中創建另一個主題,該主題會再次返回'starApp()'並立即關閉它,它可能會工作,因爲它會處理您的'require('./ lib/start- app.js')'作爲一個singleton,因此關閉它只是一個監聽器。 –

相關問題