JavaScript构造函数和测试

JavaScript构造函数和测试,javascript,Javascript,我是js新手,所以这可能是个愚蠢的问题。我写了这段代码,但测试没有通过。。我还试图在insert和contain中没有任何代码的情况下通过测试,但测试并没有再次通过。告诉我怎么了 'use strict'; class BinaryTree { constructor() { this.root = null; } insert(data) { // create new Node var newNode = new N

我是js新手,所以这可能是个愚蠢的问题。我写了这段代码,但测试没有通过。。我还试图在insert和contain中没有任何代码的情况下通过测试,但测试并没有再次通过。告诉我怎么了

'use strict';

class BinaryTree {
    constructor() {
        this.root = null;
    }

    insert(data) {
        // create new Node
        var newNode = new Node(data, null, null);

        // if tree is empty than make newNode as root
        if(this.root == null) {
            this.root = newNode;
        } else {
            // else find place where need to insert new node
            // start from root
            var currentNode = root;

            while(true) {
                // check: do we need to go left or right?
                // if left
                if(newNode.value < root.value) {
                    // if current left node is empty than insert newNode as current left node
                    if(currentNode.left == null) {
                        currentNode.left = newNode;
                        break;
                    } else {
                        // else set current node as left current node
                        currentNode = currentNode.left;
                    }
                } else if(newNode.value > root.value) {
                    // if right

                    // if current right node is empty than insert newNode as current right node
                    if(currentNode.right == null) {
                        currentNode.right = newNode;
                        break;
                    } else {
                        // else set current node as right current node
                        currentNode = currentNode.right;
                    }
                } else {
                    // if value already exist in tree than do nothing and break from while
                    break;
                }
            }
        }
    }

    contains(data) {
        var currentNode = this.root;
        var isFound = false;

        while (!isFound && currentNode != null){
            if(data < currentNode.value) {
                currentNode = currentNode.left;
            } else  if (data > currentNode.value) {
                currentNode = currentNode.right;
            } else {
                isFound = true;
            }
        }

        return isFound;
    }

    remove(data) {

    }

    size() {

    }

    isEmpty() {

    }
}
完整测试代码:

var expectedBt = {
    root: {
        data: 13,
        left: {
            data: 10,
            left: {
                data: 8,
                left: {
                    data: 6,
                    left: null,
                    right: {
                        data: 7,
                        left: null,
                        right: null
                    }
                },
                right: null
            },
            right: {
                data: 12,
                left: {
                    data: 11,
                    left: null,
                    right: null
                },
                right: null
            }
        },
        right: {
            data: 18,
            left: {
                data: 15,
                left: null,
                right: null
            },
            right: {
                data: 20,
                left: null,
                right: null
            }
        }
    }
};

describe('BinaryTree', () => {
    describe('#constructor', () => {
        it('assigns null to root', () => {
            var bt = new BinaryTree();

            expect(bt.root).to.equal(null);
        });
    });

    describe('#insert(data)', () => {
        var bt;

        beforeEach(() => {
            bt = new BinaryTree();

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);
        });

        it('creates new node with passed data and inserts it to correct place', () => {
            bt.root.data.should.equal(13);
            bt.root.left.data.should.equal(10);
            bt.root.right.data.should.equal(18);
            bt.root.left.left.data.should.equal(8);
            bt.root.left.right.data.should.equal(12);
            bt.root.right.left.data.should.equal(15);
            bt.root.right.right.data.should.equal(20);
            bt.root.left.left.left.data.should.equal(6);
            bt.root.left.left.left.right.data.should.equal(7);
            bt.root.left.right.left.data.should.equal(11);
        });
    });

    describe('#contains(data)', () => {
        var bt;

        beforeEach(() => {
            bt = new BinaryTree();

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);
        });

        it('returns true if passed data found in binary tree, otherwise if not', () => {
            bt.contains(3).should.equal(false);
            bt.contains(9).should.equal(false);
            bt.contains(16).should.equal(false);

            bt.contains(18).should.equal(true);
            bt.contains(13).should.equal(true);
            bt.contains(7).should.equal(true);
            bt.contains(20).should.equal(true);
        });
    });

    describe('#remove(data)', () => {
        var bt;
        var btCopy;

        beforeEach(() => {
            bt = new BinaryTree();

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);

            btCopy = JSON.parse(JSON.stringify(expectedBt));
        });

        it('does nothing if passed data not found', () => {
            sinon.spy(bt, 'contains');

            bt.remove(9);
            bt.should.deep.equal(btCopy);
        });

        it('removes node which contains passed data', () => {
            sinon.spy(bt, 'contains');

            bt.remove(15);
            btCopy.root.right.left = null;

            bt.should.deep.equal(btCopy);

            bt.remove(8);
            btCopy.root.left.left = btCopy.root.left.left.left;

            bt.should.deep.equal(btCopy);
        });
    });

    describe('#size()', () => {
        it('returns number of elements in tree', () => {
            var bt = new BinaryTree();

            bt.size().should.equal(0);

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);

            bt.size().should.equal(10);

            bt.remove(8);
            bt.remove(18);
            bt.remove(20);

            bt.size().should.equal(7);
        });
    });

    describe('#isEmpty()', () => {
        it('returns true if tree is empty, false if not', () => {
            var bt = new BinaryTree();
            bt.isEmpty().should.equal(true);

            bt.insert(5);
            bt.isEmpty().should.equal(false);

            bt.remove(5);
            bt.isEmpty().should.equal(true);
        });
    });
});

您是否尝试过记录
bt.root
?只是在Edge中尝试过,该类只有一个构造函数,并且在创建新的BinaryTree后,root为null。你所处的环境是什么?浏览器节点?什么版本?@Ryan O'Hara这是什么意思?你能解释一下吗?@Karl Johan Sjögren我使用原子作为节点。并在mozilla firefox浏览器中进行测试,最后更新。我尝试创建新的js文件,并仅使用构造函数创建类。在最新的Firefox开发者版中,“将null分配给root”测试对我来说也很好(虽然是手动执行的,但没有用jasmine尝试)。按照Ryan所说的做,并在测试中添加一些日志记录,看看真正发生了什么,如果你不知道javascript中的日志记录和调试,你可能应该先读一读。你是否尝试过日志记录
bt.root
?只在Edge中尝试过这个方法,该类只有一个构造函数,在创建新的BinaryTree根为null之后。你所处的环境是什么?浏览器节点?什么版本?@Ryan O'Hara这是什么意思?你能解释一下吗?@Karl Johan Sjögren我使用原子作为节点。并在mozilla firefox浏览器中进行测试,最后更新。我尝试创建新的js文件,并仅使用构造函数创建类。在最新的Firefox开发者版中,“将null分配给root”测试对我来说也很好(虽然是手动执行的,但没有用jasmine尝试)。按照Ryan所说的做,并在测试中添加一些日志记录,看看真正发生了什么,如果您不知道如何使用javascript进行日志记录和调试,那么您可能应该先了解一下。
var expectedBt = {
    root: {
        data: 13,
        left: {
            data: 10,
            left: {
                data: 8,
                left: {
                    data: 6,
                    left: null,
                    right: {
                        data: 7,
                        left: null,
                        right: null
                    }
                },
                right: null
            },
            right: {
                data: 12,
                left: {
                    data: 11,
                    left: null,
                    right: null
                },
                right: null
            }
        },
        right: {
            data: 18,
            left: {
                data: 15,
                left: null,
                right: null
            },
            right: {
                data: 20,
                left: null,
                right: null
            }
        }
    }
};

describe('BinaryTree', () => {
    describe('#constructor', () => {
        it('assigns null to root', () => {
            var bt = new BinaryTree();

            expect(bt.root).to.equal(null);
        });
    });

    describe('#insert(data)', () => {
        var bt;

        beforeEach(() => {
            bt = new BinaryTree();

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);
        });

        it('creates new node with passed data and inserts it to correct place', () => {
            bt.root.data.should.equal(13);
            bt.root.left.data.should.equal(10);
            bt.root.right.data.should.equal(18);
            bt.root.left.left.data.should.equal(8);
            bt.root.left.right.data.should.equal(12);
            bt.root.right.left.data.should.equal(15);
            bt.root.right.right.data.should.equal(20);
            bt.root.left.left.left.data.should.equal(6);
            bt.root.left.left.left.right.data.should.equal(7);
            bt.root.left.right.left.data.should.equal(11);
        });
    });

    describe('#contains(data)', () => {
        var bt;

        beforeEach(() => {
            bt = new BinaryTree();

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);
        });

        it('returns true if passed data found in binary tree, otherwise if not', () => {
            bt.contains(3).should.equal(false);
            bt.contains(9).should.equal(false);
            bt.contains(16).should.equal(false);

            bt.contains(18).should.equal(true);
            bt.contains(13).should.equal(true);
            bt.contains(7).should.equal(true);
            bt.contains(20).should.equal(true);
        });
    });

    describe('#remove(data)', () => {
        var bt;
        var btCopy;

        beforeEach(() => {
            bt = new BinaryTree();

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);

            btCopy = JSON.parse(JSON.stringify(expectedBt));
        });

        it('does nothing if passed data not found', () => {
            sinon.spy(bt, 'contains');

            bt.remove(9);
            bt.should.deep.equal(btCopy);
        });

        it('removes node which contains passed data', () => {
            sinon.spy(bt, 'contains');

            bt.remove(15);
            btCopy.root.right.left = null;

            bt.should.deep.equal(btCopy);

            bt.remove(8);
            btCopy.root.left.left = btCopy.root.left.left.left;

            bt.should.deep.equal(btCopy);
        });
    });

    describe('#size()', () => {
        it('returns number of elements in tree', () => {
            var bt = new BinaryTree();

            bt.size().should.equal(0);

            bt.insert(13);
            bt.insert(10);
            bt.insert(8);
            bt.insert(18);
            bt.insert(12);
            bt.insert(11);
            bt.insert(15);
            bt.insert(6);
            bt.insert(20);
            bt.insert(7);

            bt.size().should.equal(10);

            bt.remove(8);
            bt.remove(18);
            bt.remove(20);

            bt.size().should.equal(7);
        });
    });

    describe('#isEmpty()', () => {
        it('returns true if tree is empty, false if not', () => {
            var bt = new BinaryTree();
            bt.isEmpty().should.equal(true);

            bt.insert(5);
            bt.isEmpty().should.equal(false);

            bt.remove(5);
            bt.isEmpty().should.equal(true);
        });
    });
});