我需要一些帮助来测试 Node 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)
}
});
});
});
最佳答案
在查看它之后,错误消息让我陷入困境,我只是没有导出我的服务器。
关于node.js - 类型错误 : Cannot read property 'address' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58647678/