2015-06-27 64 views
0

我正在使用茉莉花2.3.1。在畢竟方法如下規範執行結果不會被調用:茉莉花afterAll沒有調用時拋出錯誤

var http = require('http'); 

describe("simple server tests:", function() { 

    afterAll(function(done) { 
     console.log('after all'); 
    }); 

    it("throws an error because server is not running", function(done) { 
     http.get("http://127.0.0.1:8080", function(res) { 
      res.on('data', function(data) { 
       done(); 
      }); 
     }); 
    }); 
}); 

控制檯顯示:

[23:25:24] Starting 'test'... 
events.js:85 
     throw er; // Unhandled 'error' event 
      ^
Error: connect ECONNREFUSED 
    at exports._errnoException (util.js:746:11) 
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19) 

我的預期是,該畢竟被調用,而不考慮該試驗方法拋出的錯誤。我真的不想試試我的測試。請讓我知道這是茉莉花還是我使用茉莉花的問題。謝謝。

回答

0

afterAll在所有規格已完成時被調用,但您的情況服務器未運行,因此從不調用done()。如果您正在測試服務器沒有運行,我會建議增加一個錯誤處理的請求和執行done()裏面:

var http = require('http'); 

describe("simple server tests:", function() { 

    afterAll(function(done) { 
     console.log('after all'); 
     done(); // do not forget to execute done if you use it 
    }); 

    it("throws an error because server is not running", function(done) { 

     http.get("http://127.0.0.1:8080", function(res) { 
      res.on('data', function(data) { 
       // never called 
      }) 
     }).on('error', function(e) { 
      console.log("Got error: " + e.message); 
      done(); // finish async test 
     }); 
    }); 
}); 
+0

如果做得不那麼叫我希望超時和方法畢竟被稱爲。我有這個問題,任何測試都可能因語法錯誤或意外錯誤而失敗。在這些情況下,畢竟沒有被召喚。即使發生錯誤,我也希望在所有情況下都能被調用。 – planetjones

+1

由於被測單元的異步行爲,它會以異常退出。 Jasmine會捕獲同步異常,但不會異步,因爲如果它是範圍,它們就會失效。你甚至可以嘗試'setTimeout(function(){a = b;},500);'並得到一個測試套件退出的'ReferenceError'。沒有任何東西可以捕捉到這個異常,因此整個套件都會關閉很明顯,如果套件停機,那麼'afterAll'不會被調用。我必須是[已知問題](https://github.com/jasmine/jasmine/issues/529) –

+0

也許這個模塊可以幫助你 - [茉莉花異步錯誤](https://www.npmjs。 COM /包/茉莉異步-錯誤)。 –