【发布时间】:2020-01-12 16:02:35
【问题描述】:
所以我是 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,我总是得到这个错误消息:
未捕获的类型错误:无法读取未定义的属性“状态”
ohya,我也使用 should 方法。 我收到了另一个错误消息,例如:无法读取属性应该为空
我读过这个线程。
Should js Cannot read property 'should' of null
这就是我想改变和使用期望方法的原因。
你们能帮帮我吗。
谢谢。
::: 更新 ::: 如何解决这个问题? 而不是使用这行代码:
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();
});
});
【问题讨论】:
-
这只是意味着您的
res对象未定义,因此它没有任何属性,包括status- 即您的结果不会返回。你能从命令行向你的目标发出一个简单的 GET 并确认它以 200 响应吗?
标签: javascript node.js mocha.js chai