所以我是 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/