Javascript Mocha测试:未捕获类型错误:无法读取属性';状态';空的
学习TDD和我的“Hello World”服务器响应的第一个简单测试在Mocha中失败。我正在使用Mocha.js、Superagent和Expect.js 当IJavascript Mocha测试:未捕获类型错误:无法读取属性';状态';空的,javascript,node.js,mocha.js,superagent,expect.js,Javascript,Node.js,Mocha.js,Superagent,Expect.js,学习TDD和我的“Hello World”服务器响应的第一个简单测试在Mocha中失败。我正在使用Mocha.js、Superagent和Expect.js 当Icurl-I localhost:8080时,我得到了正确的响应和状态代码 HTTP/1.1 200 OK Content-Type: text/plain Date: Mon, 27 Apr 2015 17:55:36 GMT Connection: keep-alive Transfer-Encoding: chunked Hel
curl-I localhost:8080时,我得到了正确的响应和状态代码
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
测试代码:
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();
});
});
});
服务器代码:
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");
});
摩卡咖啡产量:
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
我曾尝试用谷歌搜索这个问题,但没有发现我做错了什么。回调的节点表示法是第一个参数错误
Superagent正在遵循此节点策略。这是来自:
所以改变这条线
request.get('localhost:8080').end(function(res){
到
有人注意到expect().to.equal不再起作用了吗???图书馆的作者没有提到这些废话???顺便说一句,它从“.to.equal”改为“.toEqual”。我很惊讶这家伙的代码居然能用
request.get('localhost:8080').end(function(res){
request.get('localhost:8080').end(function(err, res){