admin管理员组

文章数量:1323715

so im newbie with mocha-chai things in nodejs env. i dont understand why i cant get the response status while running mochajs.

here is my code :

let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('server');
let expect = require("chai").expect;
let should = require("should");

let request = require("superagent");
let util = require("util");

chai.use(chaiHttp);

describe('API Clinic Test', function() {

  it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

  it('should list a SINGLE clinic on /api/v1/clinic/<id> GET');
  it('should add a SINGLE clinic on /api/v1/clinic POST');
  it('should update a SINGLE clinic on /api/v1/clinic/<id> PUT');
  it('should delete a SINGLE clinic on /api/v1/clinic/<id> DELETE');
});

everytime i run mocha test.js, i always get this error msg :

Uncaught TypeError: Cannot read property 'status' of undefined

ohya, i use should method too. i got another error msg like : cannot-read-property-should-of-null

i read on this thread.

Should js Cannot read property 'should' of null

thats why i want to change and use expect method.

can you guys please help me.

thank you.

::: update ::: how to fix the issue ? instead of using this line of codes :

it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

i use this :

it('should list ALL clinic on /api/v1/clinic GET', function(done) {
chai.request('localhost:5000') .get('/api/v1/clinic') 
.end(function(err, res){
            // res.should.have.status(200);
            expect(res.status).to.equal(200);
          done();
        });
      });

so im newbie with mocha-chai things in nodejs env. i dont understand why i cant get the response status while running mochajs.

here is my code :

let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('server');
let expect = require("chai").expect;
let should = require("should");

let request = require("superagent");
let util = require("util");

chai.use(chaiHttp);

describe('API Clinic Test', function() {

  it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

  it('should list a SINGLE clinic on /api/v1/clinic/<id> GET');
  it('should add a SINGLE clinic on /api/v1/clinic POST');
  it('should update a SINGLE clinic on /api/v1/clinic/<id> PUT');
  it('should delete a SINGLE clinic on /api/v1/clinic/<id> DELETE');
});

everytime i run mocha test.js, i always get this error msg :

Uncaught TypeError: Cannot read property 'status' of undefined

ohya, i use should method too. i got another error msg like : cannot-read-property-should-of-null

i read on this thread.

Should js Cannot read property 'should' of null

thats why i want to change and use expect method.

can you guys please help me.

thank you.

::: update ::: how to fix the issue ? instead of using this line of codes :

it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

i use this :

it('should list ALL clinic on /api/v1/clinic GET', function(done) {
chai.request('localhost:5000') .get('/api/v1/clinic') 
.end(function(err, res){
            // res.should.have.status(200);
            expect(res.status).to.equal(200);
          done();
        });
      });
Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Jan 5, 2017 at 18:49 gutasaputragutasaputra 1791 gold badge7 silver badges20 bronze badges 1
  • This just means that your res object is undefined, so it doesn't have any properties, including status -- i.e., your result isn't ing back. can you issue a simple GET to your target from the mand line and confirm that it responds with a 200? – meatspace Commented Jan 5, 2017 at 18:53
Add a ment  | 

2 Answers 2

Reset to default 2

you are hitting an error most likely...you should have a line similar to the below

if(err) done(err);

Per ments...this led you in the right direction. Moreso you needed to do the below:

chai.request('http://localhost:5000').get('/api/v1/clinic')

In my case I forgot to export module like export defaul mymodule. So check for this.

本文标签: javascriptCannot read property 39status39 of undefinedMocha ChaiStack Overflow