javascript - 无法读取未定义的属性 'status' - Mocha Chai

标签 javascript node.js mocha.js chai

所以我是 Nodejs 环境中 Mocha Chai 事物的新手。 我不明白为什么我在运行 mochajs 时无法获取响应状态。

这是我的代码:

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

每次我运行 mocha test.js 时,我总是收到此错误消息:

Uncaught TypeError: Cannot read property 'status' of undefined

ohya,我也用should方法。 我收到另一个错误消息,例如:cannot-read-property-should-of-null

我读过这个帖子。

Should js Cannot read property 'should' of null

这就是为什么我想改变并使用expect方法。

你们能帮帮我吗?

谢谢。

:::更新::: 如何解决这个问题? 而不是使用这行代码:

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

最佳答案

您很可能遇到错误...您应该有类似于下面的行

if(err) done(err);

根据评论...这引导您走向正确的方向。此外,您需要执行以下操作:

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

关于javascript - 无法读取未定义的属性 'status' - Mocha Chai,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41492477/

相关文章:

node.js - 如何从 io.use 设置或删除 cookie

node.js - 在 Express 中,为什么 .config() 中的 env 变量是可选的?

selenium-webdriver - 如何在 typescript 测试中增加每个套件的 Mocha 超时

node.js - 如何在 IntelliJ IDEA 13(或 WebStorm)上远程运行 mocha 测试?

javascript - 无限平铺网页

javascript - 滚动后继续显示字幕内容

javascript - TypeError : chunk. isInitial 不是函数

node.js - 如何使用 local.example.js ENV 运行测试

javascript - 检测手指何时从触摸板上移开,但由于 "predictive touch"- JS,滚动仍然发生

javascript - 有什么用!!在 JavaScript 中