【问题标题】:Testing GET endpoint, using mocha and chai, AssertionError测试 GET 端点,使用 mocha 和 chai,AssertionError
【发布时间】:2018-11-05 10:54:11
【问题描述】:

我正在尝试测试我的 GET 端点,但收到此错误:

  Uncaught AssertionError: expected { Object (_bsontype, id) } to equal '5be02038cf97ed1cc47feb8a'

我的测试是:

  it("it should GET a user by the given id", done => {
        let newUser = new User({
            email: "bloggs@test.com",
            givenName: "Willmott",
            familyName: "Bloggs"
        });

        newUser.save((err, newUser) => {
            let { _id: id } = newUser;

            chai
                .request(app)
                .get("/users/" + id)
                .send(newUser)
                .end((err, res) => {
                    let { body: { _id } } = res;

                    expect(id).to.equal(_id);
                    done();
                });
        });
    });

我怎样才能让expect(id).to.equal(_id); 通过?

【问题讨论】:

    标签: node.js mocha.js chai


    【解决方案1】:

    看起来其中一个是对象 (_id),另一个是字符串 (id)。您可能不想比较整个对象,而是比较对象上的某些属性。考虑到这看起来像是使用 bson 对象的数据库查找,我猜你可能可以调用 toString() 吗?至少,如果这实际上是MongoDB UserId

    expect(id).to.equal(_id.toString());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      相关资源
      最近更新 更多