我需要一些有关测试节点js代码的帮助。我正在使用mocha和supertest,但不断收到与测试标题相同的错误。我还使用mongoose连接到mongo服务器,在运行测试之前,先添加了一些guest对象。我已经为此感到困惑2天了,不胜感激。
这是测试错误
0 passing (96ms)
1 failing
1) Guestss
GET /guests
should GET all the guests:
TypeError: Cannot read property 'address' of undefined
at Test.serverAddress (node_modules/supertest/lib/test.js:55:18)
at new Test (node_modules/supertest/lib/test.js:36:12)
at Object.get (node_modules/supertest/index.js:25:14)
at Context.<anonymous> (test/functional/api/guestTest.js:95:18)
这是测试代码
describe("GET /guests", () => {
it("should GET all the guests", done => {
console.log("before");
request(server)
.get("/guests")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200)
.end((err, res) => {
try {
expect(res.body).to.be.a("array");
expect(res.body.length).to.equal(2);
let result = _.map(res.body, guest => {
return {
name: guest.name,
people: guest.people,
roomno: guest.roomno,
breakfast: guest.breakfast,
roomtype: guest.roomtype,
check: guest.check
}
});
expect(result).to.deep.include({
name: "Tommy blue",
people: 5,
roomno: 0,
breakfast: true,
roomtype: "family",
check: "waiting"
});
expect(result).to.deep.include({
name: "Chad Warren",
people: 2,
roomno: 45,
breakfast: true,
roomtype: "double",
check: "in"
});
done()
} catch (e) {
done(e)
}
});
});
});
答案 0 :(得分:0)
查看后,错误消息使我大吃一惊,而我只是没有导出服务器。
答案 1 :(得分:0)
这里也一样。我的考试有
const { app } = require('./app');
我的应用有
module.exports = app;
我不得不更改app.js行以使其具有大括号,就像测试中那样:
module.exports = { app };