0
我的文件結構是這樣的無法獲取節點JS測試失敗
folder_main
-server.js
-folder_tests
--serverTest.js
var expect = require("chai").expect;
var http = require('http')
describe('/', function(){
var server;
beforeEach(function() {
server = require('../server');
});
afterEach(function() {
server.close();
});
it('should return 200 status code', function(){
http.get("http://localhost:8080", function(res) {
console.log("Got response: " + res.statusCode);
expect(res.statusCode).to.equal("This isnt even a number")
})
})
})
和server.js是
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = 8080;
server.listen(port);
console.log("listening on port " + port)
// router
app.get('/', function (req, res){
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("Hello World\n");
});
module.exports = server;
當我從CMD線運行「摩卡測試」我得到
✓ should return 200 status code
1) "after each" hook
1 passing (277ms) 1 failing
1)/"after each" hook:
Uncaught Error: connect ECONNRESET
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
我很困惑
第一個測試應該失敗,因爲它比作「這個心不是連號」。
我不知道什麼是與未被捕獲的錯誤發生:連接ECONNRESET
謝謝馬特的深刻見解。您已正確識別問題,並將完成的事情傳遞給最外層函數,並在http.get()函數中調用它會導致測試失敗。祝你有美好的一天! – humanbeing