【问题标题】:TypeError: Cannot read property 'address' of undefinedTypeError:无法读取未定义的属性“地址”
【发布时间】:2020-02-27 01:39:26
【问题描述】:

我需要一些帮助来测试节点 js 代码。我正在使用 mocha 和 supertest 并且我不断收到与我的测试标题相同的错误。我还在使用 mongoose 连接到 mongo 服务器,并且在运行测试之前添加了一些来宾对象。我已经为此困扰了 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)
                    }
                });
        });
    });

【问题讨论】:

  • 也许 .expect("Content-Type", /json/) 应该是 .expect("Content-Type", "json")

标签: node.js mongoose mocha.js supertest


【解决方案1】:

在调查之后,错误消息发送给我,而我只是没有导出我的服务器。

【讨论】:

    【解决方案2】:

    这里也一样。我的测试有

    const { app } = require('./app');
    

    我的应用有

    module.exports = app;  
    

    我不得不将 app.js 行更改为像测试那样使用花括号:

    module.exports = { app };
    

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2022-01-14
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2022-01-12
      相关资源
      最近更新 更多