简体   繁体   中英

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. 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. 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. This is because they really are two separate objects stored in memory that happen to look alike.

Use to.deep.equal instead!

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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