node.js - 类型错误 : Cannot read property 'address' of undefined

标签 node.js mongoose mocha.js supertest

我需要一些帮助来测试 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/

相关文章:

node.js - 在集群模式下运行 pm2 时重定向后 req.session 数据丢失

javascript - 使用 mongodb 和 node js 制作一个有历史的聊天系统

javascript - 将multer文件(nodejs)上传到Mongo数据库

node.js - 基于 http 调用测试 Web 应用程序的最佳实践

javascript - 有没有办法结束thenable链?

javascript - 处理并保持 node.js/express session

mongodb - 使用 $elemMatch 更新 mongodb 嵌入式文档

node.js - 当数据库中存在空字段时,为什么 Mongoose 会响应空字段

javascript - 浏览器中的 Mocha : How to get a report using chai. 断言

webpack - mocha-webpack 引用错误 : window is not defined