简体   繁体   English

摩卡/柴断言失败,但没有区别

[英]Mocha/chai assertion is failing but there is no diff

I'm creating a binary tree class in javascript and my test is failing but I don't see anything wrong with my method and I'm not receiving any diff. 我正在用javascript创建一个二叉树类,我的测试失败了,但是我的方法没有发现任何问题,并且没有收到任何差异。 Any insight would be awesome. 任何见解都会很棒。

Here's my class: 这是我的课:

    function binaryTree() {
      this.root = null;
    };
    binaryTree.prototype = {
    constructor: binaryTree,
    add: function(val) {
        var root = this.root;
        if(!root) {
          this.root = new Node(val);
          return;
        }
        var currentNode = root;
        var newNode = new Node(val);
        while(currentNode) {
          if(val < currentNode.value) {
            if(!currentNode.left) {
              currentNode.left = newNode;
              break;
            }
            else {
              currentNode = currentNode.left;
            }
          }
          else {
            if(!currentNode.right) {
              currentNode.right = newNode;
              break;
            }
            else {
              currentNode = currentNode.right;
            }
          }
        }
      }

Here's my test: 这是我的测试:

it('adds values to the binary tree', function () {
  var test = new binaryTree();
  test.add(7);
  test.add(43);
  test.add(13);
  test.add(27);
  test.add(82);
  test.add(2);
  test.add(19);
  test.add(8);
  test.add(1);
  test.add(92);

  expect(test).to.equal({
    root:
     { value: 7,
       left:
        { value: 2,
          left: { value: 1, left: null, right: null },
          right: null },
       right:
        { value: 43,
          left:
           { value: 13,
             left: { value: 8, left: null, right: null },
             right:
              { value: 27,
                left: { value: 19, left: null, right: null },
                right: null } },
          right:
           { value: 82,
             left: null,
             right: { value: 92, left: null, right: null } } } }
  });
});

And here is the error I'm getting: 这是我得到的错误:

1) binary tree tests adds values to the binary tree:

    AssertionError: expected { Object (root) } to equal { Object (root) }
    + expected - actual

If I mess around with the values in the test object I see a diff appear so it looks to me like everything is equal and I'm stumped. 如果我弄乱了测试对象中的值,我会看到一个差异,因此在我看来一切都相等,我很困惑。 I'd really appreciate it if I could get a second pair of eyes on this. 如果能再引起我的关注,我将不胜感激。

You are using Mocha's to.equal expectation, but that tests for strict equality. 您正在使用Mocha的to.equal期望,但这会测试严格的均等性。 http://chaijs.com/api/bdd/#method_equal http://chaijs.com/api/bdd/#method_equal

Two objects, even if they have all the same key-value pairs, will not return true to a triple-equals (===) comparator. 即使两个对象具有相同的键值对,它们也不会对三等式(===)比较器返回true。 This is because they really are two separate objects stored in memory that happen to look alike. 这是因为它们实际上是存储在内存中的两个单独的对象,它们看起来相似。

Use to.deep.equal instead! 使用to.deep.equal代替!

Make sense? 说得通?

In case anyone comes across this I found the issue. 万一遇到任何人,我发现了问题。 Two objects cannot be perfectly compared in JavaScript even if all of the properties are equal. 即使所有属性都相等,也无法在JavaScript中完美地比较两个对象。 This post has two methods to work around this limitation, one of which was very easy to implement in this case: 这篇文章有两种方法可以解决此限制,在这种情况下,其中一种很容易实现:

Changing expect(test).to.equal({ to expect(JSON.stringify(test)).to.equal(JSON.stringify({ will allow this test to pass. Stringifying the objects is a very easy way to compare two objects but this will only work if the properties are in the exact same order. expect(test).to.equal({更改为expect(test).to.equal({ expect(JSON.stringify(test)).to.equal(JSON.stringify({将允许此测试通过。对对象进行字符串化是比较两个对象的一种非常简单的方法但这仅在属性的顺序完全相同时才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM