[英]How does Mocha execute the 'it' calls? How to execute tests synchronously?
观察以下测试:
describe("Testing i", function(){
var i = 0;
it('i should be 0', function() {
expect(i).to.equal(0);
});
i++;
it('i should be 1', function() {
expect(i).to.equal(1);
});
i= 7;
it('i should be 7', function() {
expect(i).to.equal(7);
});
});
前两个测试失败。 但是我不知道为什么! 我究竟做错了什么?
测试显示以下内容:
我正在将QUnit移植到Mocha,并且正在寻找断言应该放在哪里。
在QUnit中,您具有模块->测试->断言。
是这里描述->描述->是吗?
您做错了测试。 整个测试,变量设置以及所有功能都必须包含在it
功能范围之内。
it
功能是将您的测试推送到将由describe
函数或mocha本身执行的列表。 it
功能不执行测试。
编写测试的正确方法如下:
describe("Testing i", function(){
it('i should be 0', function() {
var i = 0;
expect(i).to.equal(0);
});
it('i should be 1', function() {
var i = 0;
i++;
expect(i).to.equal(1);
});
it('i should be 7', function() {
var i = 0;
i = 7;
expect(i).to.equal(7);
});
});
FWIW。 除非您将参数传递给it
函数的回调,否则所有测试将同步执行。 只是它们没有在describe
函数中执行。 在it
的功能只是编译所有it
调用到的测试列表。
因此,如果您需要执行一系列操作,则可以执行以下操作:
describe("Testing i", function(){
var i;
it('i should be 0', function() {
i = 0;
expect(i).to.equal(0);
});
it('i should be 1', function() {
i++;
expect(i).to.equal(1);
});
it('i should be 7', function() {
i = 7;
expect(i).to.equal(7);
});
});
但是请注意,不建议这样做,因为如果第一个测试失败,则随后的某些测试也可能会失败。 在此示例中,如果第一个测试由于i
的值不为0而失败,则第二个测试也将失败。 但是,此模式对于执行时间较长的步骤(例如,网站登录)可能很有用。
这使mocha可以进行进度条等类似的花式测试报告。在不知道要运行多少个测试(一项测试是it
功能)的情况下,mocha无法知道绘制进度条的百分比。
它还允许摩卡(mocha)对错误消息进行精美的格式化。 如果您使用过mocha,则会注意到mocha会收集所有错误消息(失败)并在最后打印它们。 为此,mocha要做的是一次以受控方式执行一次测试( it
起作用),并收集抛出的所有错误。 如果您仅依靠JavaScript语法,除非您用C语言破解了解释器,否则这是不可能的。但是,如果您在数组中有一系列函数并一次执行一个函数,则可以这样做。
回答问题,执行流程如何工作。 摩卡咖啡从描述块开始。
首先在describe块中,它执行不在it
块中的所有代码。
因此,在你的代码首先这3条语句执行之前执行it
的块
var i = 0;
i++;
i=7;
分配给i的最后一个值是7。现在它将开始执行它的块。
类似于Mocha文档 https://mochajs.org/ 上的解决方案
describe("Testing i", function(){
var i;
it('i should be 0', function() {
i = 0;
expect(i).to.equal(0);
});
it('i should be 1', function() {
i++;
expect(i).to.equal(1);
});
it('i should be 7', function() {
i= 7;
expect(i).to.equal(7);
});
});
问题中要求的呼叫顺序
我使用本文http://cwinters.com/2014/09/26/mocha-nested-hook-ordering.html中的改编代码对其进行了测试
这是我使用的代码
'use strict';
describe('level 1', function() {
before(function() { console.log("L1 - before") });
beforeEach(function() { console.log("L1 - beforeEach") });
after(function() { console.log("L1 - after") });
afterEach(function() { console.log("L1 - afterEach") });
console.log("inner DESCRIBE BEFORE L1A") // equivalent to asigning the variable the first time
it('L1 test A', function() {});
console.log("inner DESCRIBE BEFORE L1B") // equivalent to asigning the variable the second time
it('L1 test B', function() {});
describe('level 2', function() {
before(function() { console.log("L2 - before") });
beforeEach(function() { console.log("L2 - beforeEach") });
after(function() { console.log("L2 - after") });
afterEach(function() { console.log("L2 - afterEach") });
it('L2 test A', function() {});
it('L2 test B', function() {});
});
});
结果在这里
inner DESCRIBE BEFORE L1A // execute First
inner DESCRIBE BEFORE L1B // execute Second
level 1 // now the test are executed
L1 - before
L1 - beforeEach
√ L1 test A
L1 - afterEach
L1 - beforeEach
√ L1 test B
L1 - afterEach
level 2
L2 - before
L1 - beforeEach
L2 - beforeEach
√ L2 test A
L2 - afterEach
L1 - afterEach
L1 - beforeEach
L2 - beforeEach
√ L2 test B
L2 - afterEach
L1 - afterEach
L2 - after
L1 - after
4 passing (64ms)
=> describe
块将立即执行,因此在调用任何其他函数之前,最后一个变量将覆盖对同一变量的多个变量赋值。 前两个测试失败,因为在执行所有三个测试时i==7
。
如果确实需要该序列,则可以使用闭包,但这会增加很多复杂性 。
看起来下面是要走的路:
describe("Testing i", function(){
it('i increments', function(){
var i = 0;
expect(i, 'i should be 0').to.equal(0);
i++;
expect(i, 'i should be 1').to.equal(1);
i = 7;
expect(i, 'i should be 7').to.equal(7);
});
});
QUnit.module
映射来describe
, QUnit.test
映射到it
并且assert
映射以expect
。
我已经写了一个关于以下主题的博客: 将QUnit测试转换为Mocha / Chai 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.