当使用 chai 进行单元测试时,我不断收到此错误消息,但没有一个测试通过,即使它们应该通过。在这种情况下是什么意思? 谢谢。
var chai = require('chai');
var chaiHttp = require('chai-http');
var server = require('../server.js');
var should = chai.should();
var app = server.app;
var storage = server.storage;
chai.use(chaiHttp);
describe('Shopping List', function() {
it('should list items on GET', function(done) {
chai.request(app)
.get('/items')
.end(function(err, res) {
res.should.have.status(200);
res.should.be.json; // jshint ignore:line
res.body.should.be.a('array');
res.body.should.have.length(3);
res.body[0].should.be.a('object');
res.body[0].should.have.property('id');
res.body[0].should.have.property('name');
res.body[0].id.should.be.a('number');
res.body[0].name.should.be.a('string');
res.body[0].name.should.equal('Broad beans');
res.body[1].name.should.equal('Tomatoes');
res.body[2].name.should.equal('Peppers');
done();
});
});
完整错误信息:
1) Shopping List should list items on GET:
TypeError: Cannot read property 'address' of undefined
at serverAddress (/home/ubuntu/workspace/thinkful-node-course/unit2/node_modules/chai-http/lib/request.js:252:17)
at new Test (/home/ubuntu/workspace/thinkful-node-course/unit2/node_modules/chai-http/lib/request.js:244:53)
at Object.obj.(anonymous function) [as get] (/home/ubuntu/workspace/thinkful-node-course/unit2/node_modules/chai-http/lib/request.js:216:14)
at Context.<anonymous> (test-server.js:14:8)
最佳答案
而不是使用应用程序尝试使用:
chai.request('http://localhost:1234')
.get('/items')
关于javascript - 使用 Chai 进行单元测试时,未定义的 "TypeError: Cannot read property ' 地址'是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39544239/