javascript - Mocha Test: Uncaught TypeError: Cannot read property 'status' of null - Stack Overflow

Learning TDD and my first simple test for my "Hello World" server response is failing in Moch

Learning TDD and my first simple test for my "Hello World" server response is failing in Mocha. I'm using Mocha.js, Superagent, & Expect.js.

When I curl -i localhost:8080, I get the correct response and status code.

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

Test code:

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();
        });
    });
});

Server code:

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");
});

Mocha output:

  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

I've tried googling this issue but no luck finding out what I'm doing wrong.

Learning TDD and my first simple test for my "Hello World" server response is failing in Mocha. I'm using Mocha.js, Superagent, & Expect.js.

When I curl -i localhost:8080, I get the correct response and status code.

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

Test code:

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();
        });
    });
});

Server code:

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");
});

Mocha output:

  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

I've tried googling this issue but no luck finding out what I'm doing wrong.

Share Improve this question edited Sep 26, 2015 at 16:31 luboskrnac 24.6k10 gold badges86 silver badges93 bronze badges asked May 1, 2015 at 2:25 metamemetame 2,6401 gold badge20 silver badges23 bronze badges 1
  • has anybody noticed that expect().to.equal doesnt work anymore??? and that the library authors make no mention of this crap??? BTW its changed from ".to.equal" to ".toEqual". I'm surprised this guy's code works at all – enorl76 Commented Nov 18, 2015 at 21:07
Add a ment  | 

1 Answer 1

Reset to default 10

Node notation of callbacks is to have first parameter error.

Superagent is following this Node policy. This is from superagent github site:

request
  .post('/api/pet')
  .send({ name: 'Manny', species: 'cat' })
  .set('X-API-Key', 'foobar')
  .set('Accept', 'application/json')
  .end(function(err, res){
    // Calling the end function will send the request
  });

So change this line

request.get('localhost:8080').end(function(res){

to

request.get('localhost:8080').end(function(err, res){

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743670857a4487756.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信