【问题标题】:Mocha chai unexpected assertion testing error from object equaling object [duplicate]来自等于对象的对象的 Mocha chai 意外断言测试错误 [重复]
【发布时间】:2016-10-19 06:57:44
【问题描述】:

我正在使用 Mocha 和 Chai 进行一些前端测试并放入以下断言。

  it('AddContact returns contact with type = ADD_CONTACT', function () {
    function hi() {return {
      type: 'ADD_CONTACT',
      data: {
        firstName: 'John',
        lastName: 'Doe',
        dateOfBirth: '1/2/89',
        phone: '123-456-7890',
        email: 'jdoe@gmail.com',
        notes: 'Most original name ever'
      }}}
    expect(hi()).to.equal({
      type: 'ADD_CONTACT',
      data: {
        firstName: 'John',
        lastName: 'Doe',
        dateOfBirth: '1/2/89',
        phone: '123-456-7890',
        email: 'jdoe@gmail.com',
        notes: 'Most original name ever'
      }
    });
  });

但是,我仍然收到错误:

  AssertionError: expected { Object (type, data) } to equal { Object (type, data) }
  + expected - actual

发生了什么事?

【问题讨论】:

    标签: javascript unit-testing mocha.js chai


    【解决方案1】:

    您必须使用 to.deep.equal 否则您正在测试的是内存中的 same 对象,而不是值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多