将mocha.js和chai.js基本但正确地使用beforeEach()或afterEach()

克怀特

我想使用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++;
  }
};

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

佐伊卜·伊亚兹(Zohaib Ijaz)

未定义,因为它不在测试功能范围内。在描述范围之前先定义它。作为参考,请查看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);
  });

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将mocha.js和chai.js基本但正确地使用beforeEach()或afterEach()

来自分类Dev

使用Mocha,Chai,chaiAsPromised和Sinon测试JS Promise

来自分类Dev

我如何期望在Node.js中使用sinon,mocha和chai的特定参数调用函数?

来自分类Dev

使用Mocha / Chai测试JS异常

来自分类Dev

使用 Mocha 和 Chai 测试 React

来自分类Dev

如何正确测试将axios.get和cheerios.load函数与mocha和chai结合使用的函数?

来自分类Dev

如何使用fs在node.js Express上正确地将数组写入文件?

来自分类Dev

正确地将PagerJS与History.js集成

来自分类Dev

Chai versus should.js with Mocha for Node.js

来自分类Dev

Chai vs.should.js与Mocha for Node.js

来自分类Dev

使用Mocha Chai测试node.js中引发的错误

来自分类Dev

使用Mocha和Chai捕捉范围外的错误

来自分类Dev

如何使用chai和mocha测试子进程?

来自分类Dev

使用setInterval测试功能时,Mocha和Chai测试失败

来自分类Dev

如何使用chai和mocha验证抛出的JavaScript异常?

来自分类Dev

如何使用chai和mocha测试数组数组中的元素?

来自分类Dev

使用 mocha 和 chai 测试对象数组中的对象键

来自分类Dev

如何使用 webdriverIO、Mocha 和 Chai 验证元素已消失

来自分类Dev

使用 Mocha 和 chai 库测试 nodejs 方法

来自分类Dev

如何在Mocha / Chai中测试JS原型(非模块)?

来自分类Dev

如何使用 Mocha、Chai 和 Sinon 正确测试 Express 控制器方法

来自分类Dev

如何正确地将图像发布到alchemy node.js服务器?

来自分类Dev

正确地将class_attribute和hash一起使用

来自分类Dev

如何使用ReactJS将图像正确地适合HTML和CSS中的div

来自分类Dev

如何使用Mocha设置Nightwatch.js以使用chai断言?

来自分类Dev

将chai-as-promised和chai-bignumber一起使用会失败

来自分类Dev

从命令行使用Chai和TypeScript运行Mocha测试

来自分类Dev

使用Mocha和Chai为具有错误的功能编写测试用例

来自分类Dev

如何使用Mocha和Chai断言验证嵌套的响应对象

Related 相关文章

  1. 1

    将mocha.js和chai.js基本但正确地使用beforeEach()或afterEach()

  2. 2

    使用Mocha,Chai,chaiAsPromised和Sinon测试JS Promise

  3. 3

    我如何期望在Node.js中使用sinon,mocha和chai的特定参数调用函数?

  4. 4

    使用Mocha / Chai测试JS异常

  5. 5

    使用 Mocha 和 Chai 测试 React

  6. 6

    如何正确测试将axios.get和cheerios.load函数与mocha和chai结合使用的函数?

  7. 7

    如何使用fs在node.js Express上正确地将数组写入文件?

  8. 8

    正确地将PagerJS与History.js集成

  9. 9

    Chai versus should.js with Mocha for Node.js

  10. 10

    Chai vs.should.js与Mocha for Node.js

  11. 11

    使用Mocha Chai测试node.js中引发的错误

  12. 12

    使用Mocha和Chai捕捉范围外的错误

  13. 13

    如何使用chai和mocha测试子进程?

  14. 14

    使用setInterval测试功能时,Mocha和Chai测试失败

  15. 15

    如何使用chai和mocha验证抛出的JavaScript异常?

  16. 16

    如何使用chai和mocha测试数组数组中的元素?

  17. 17

    使用 mocha 和 chai 测试对象数组中的对象键

  18. 18

    如何使用 webdriverIO、Mocha 和 Chai 验证元素已消失

  19. 19

    使用 Mocha 和 chai 库测试 nodejs 方法

  20. 20

    如何在Mocha / Chai中测试JS原型(非模块)?

  21. 21

    如何使用 Mocha、Chai 和 Sinon 正确测试 Express 控制器方法

  22. 22

    如何正确地将图像发布到alchemy node.js服务器?

  23. 23

    正确地将class_attribute和hash一起使用

  24. 24

    如何使用ReactJS将图像正确地适合HTML和CSS中的div

  25. 25

    如何使用Mocha设置Nightwatch.js以使用chai断言?

  26. 26

    将chai-as-promised和chai-bignumber一起使用会失败

  27. 27

    从命令行使用Chai和TypeScript运行Mocha测试

  28. 28

    使用Mocha和Chai为具有错误的功能编写测试用例

  29. 29

    如何使用Mocha和Chai断言验证嵌套的响应对象

热门标签

归档