[英]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.