摩卡柴意外断言错误

时间:2018-04-05 03:23:36

标签: javascript unit-testing mocha chai

我有一个mocha / chai断言给我一个错误,尽管值是相同的

enter image description here

测试代码在这里:

describe("Util.SplitNumAndOper Tests", function(){
    it('should have "6+4+3" return [6,"+",4,"+",3]', function(){
        let a = util.splitNumAndOper("6+4+3");
        assert.equal(a,[6,'+',4,'+',3]);
    })
})

这里发生了什么?

1 个答案:

答案 0 :(得分:0)

.equal(actual, expected, [message])方法断言实际和预期的非严格相等性(==

.deepEqual(actual, expected, [message])断言实际与预期完全相同

在您的情况下,actualexpected的值均为Array,它们引用不同的数组。它们在内存中具有不同的指针。因此.equal将失败。

import { assert } from "chai";

describe("Util.SplitNumAndOper Tests", function(){
  it('should have "6+4+3" return [6,"+",4,"+",3]', function(){
    const  a = [6,'+',4,'+',3];
    assert.equal(a,[6,'+',4,'+',3]);
  })
})

单元测试结果:

  Util.SplitNumAndOper Tests
    ✓ should have "6+4+3" return [6,"+",4,"+",3]


  1 passing (75ms)