使用mocha.js和chai.js对beforeEach()或afterEach()进行基本但正确的使用

时间:2016-06-26 23:28:59

标签: javascript testing mocha chai

我想使用mocha / chai来测试与二叉搜索树相关的代码。在这里,我正在测试公共insert方法。我希望在每个beforeEach()语句之前使用afterEach()和/或it()挂钩来重置测试环境,这样我就不必完全重复基础知识了。但是,我不断遇到各种错误。

规格

describe("BinarySearchTree insert function", function() {

  beforeEach(function() {
    var binarySearchTree = new BinarySearchTree();
    binarySearchTree.insert(5);
  });

  it("creates a root node with value equal to the first inserted value", function () {
    expect(binarySearchTree.root.value).to.equal(5);
  });

  it("has a size equal to the amount of inserted values", function () {
    binarySearchTree.insert(3);
    expect(binarySearchTree.size).to.equal(2);
  });

  it("returns an error for non-unique values", function () {
    binarySearchTree.insert(3);
    expect(binarySearchTree.insert(3)).to.throw(String);
  });

  it("if inserted value is larger than current node, make or descend to rightChild", function () {
    binarySearchTree.insert(3);
    binarySearchTree.insert(10);
    binarySearchTree.insert(7);
    expect(binarySearchTree.root.rightChild.value).to.equal(10);
  });

});

错误:ReferenceError: binarySearchTree is not defined

事实上,在没有afterEach()重置测试环境之前我预计错误,而不是因为没有定义binarySearchTree。我想完成这个,如果可能的话,只有Mocha和Chai(而不是像Sinon等其他软件包)。

经过测试的代码

exports.Node = Node;

function Node(value) {
  this.value = value;
  this.leftChild = null;
  this.rightChild = null;
}

exports.BinarySearchTree = BinarySearchTree;

function BinarySearchTree() {
  this.root = null;
  this.size = 0;
}

BinarySearchTree.prototype.insert = function(value) {
  // 1) when root node is already instantiated
  if (this.root === null) {
    // tree is empty
    this.root = new Node(value);
    this.size++;
  } else {
  // 2) nodes are already inserted
    var findAndInsert = function (currentNode) {
      if (value === currentNode.value) {
        throw new Error('must be a unique value');
      }
      // base case
      if (value > currentNode.value) {
        // belongs in rightChild
        if (currentNode.rightChild === null) {
          currentNode.rightChild = new Node(value);
        } else {
          findAndInsert(currentNode.rightChild);
        }
      } else if (value < currentNode.value) {
        // belongs in leftChild
        if (currentNode.leftChild === null) {
          currentNode.leftChild = new Node(value);
        } else {
          findAndInsert(currentNode.leftChild);
        }
      }
    };
    findAndInsert(this.root);
    this.size++;
  }
};

奖金问题...我不确定我是否正在测试抛出的错误(当插入非唯一值时)?

2 个答案:

答案 0 :(得分:2)

未定义,因为它不在测试功能范围内。在描述范围之前定义它一步。作为参考,请查看Angular文档。 https://docs.angularjs.org/guide/unit-testing

describe("BinarySearchTree insert function", function() {
  var binarySearchTree;
  beforeEach(function() {
    binarySearchTree = new BinarySearchTree();
    binarySearchTree.insert(5);
  });

  it("creates a root node with value equal to the first inserted value", function () {
    expect(binarySearchTree.root.value).to.equal(5);
  });

  it("has a size equal to the amount of inserted values", function () {
    binarySearchTree.insert(3);
    expect(binarySearchTree.size).to.equal(2);
  });

  it("returns an error for non-unique values", function () {
    binarySearchTree.insert(3);
    expect(binarySearchTree.insert(3)).to.throw(String);
  });

  it("if inserted value is larger than current node, make or descend to rightChild", function () {
    binarySearchTree.insert(3);
    binarySearchTree.insert(10);
    binarySearchTree.insert(7);
    expect(binarySearchTree.root.rightChild.value).to.equal(10);
  });

});

答案 1 :(得分:2)

您获得binarySearchTree undefined的原因已经解释过了。

我想谈谈beforeEachafterEach函数。

beforeEach函数中,我们通常会对变量初始化,存根外部调用等测试事项进行基本设置。

和&#39; afterEach&#39;方法就像destroy函数一样,通常我们编写内存释放的东西。