Tìm hiểu TDD và thử nghiệm đơn giản đầu tiên của tôi cho phản hồi máy chủ "Hello World" của tôi không thành công trong Mocha. Tôi đang sử dụng Mocha.js, Superagent, & Expect.js.Mocha Test: Uncaught TypeError: Không thể đọc thuộc tính 'status' của null
Khi tôi curl -i localhost:8080
, tôi nhận được phản hồi và mã trạng thái chính xác.
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Mon, 27 Apr 2015 17:55:36 GMT
Connection: keep-alive
Transfer-Encoding: chunked
Hello World
mã kiểm tra:
var request = require('superagent');
var expect = require('expect.js');
// Test structure
describe('Suite one', function(){
it("should get a response that contains World",function(done){
request.get('localhost:8080').end(function(res){
// TODO check that response is okay
expect(res).to.exist;
expect(res.status).to.equal(200);
expect(res.body).to.contain('World');
done();
});
});
});
đang Server:
var server = require('http').createServer(function(req, res){
res.writeHead(200, {"Content-Type":"text/plain"});
res.end('Hello World\n');
});
server.listen(8080, function(){
console.log("Server listening at port 8080");
});
đầu ra Mocha:
Suite one
1) should get a response that contains World
0 passing (110ms)
1 failing
1) Suite one should get a response that contains World:
Uncaught TypeError: Cannot read property 'status' of null
at test.js:10:23
at _stream_readable.js:908:16
Tôi đã thử googling vấn đề này nhưng không có may mắn tìm ra những gì Tôi đang làm sai.
có ai nhận thấy rằng mong đợi() to.equal doesnt làm việc nữa ??? và rằng các tác giả thư viện không đề cập đến crap này ??? BTW của nó thay đổi từ ".to.equal" thành ".toEqual". Tôi ngạc nhiên khi mã của anh chàng này hoạt động ở tất cả – enorl76