[英]How does Mocha execute the 'it' calls? How to execute tests synchronously?
[英]Why does Mocha not execute tests found in .then statements?
我正在使用Mocha / Chai開發一個Protractor測試套件(是的,我在Jasmine中遇到了同樣的問題)。
因為應用程序相當復雜,我希望能夠干燥地設置測試套件,允許我將操作鏈接到功能中。 (即,“登錄,然后瀏覽到[parameterX],然后瀏覽到[parameterY],然后期望第一個帖子標題為[parameterZ])。
但是當我把它們放在.then()語句中時,我似乎遇到了讓Mocha運行測試的麻煩。
這是一個小代碼片段,顯示了我的意思。
var chai = require('chai');
var cAP = require('chai-as-promised')
chai.use(cAP);
const expect = chai.expect;
const doTest = (x) => {
describe('main', function() {
it('should return foo', function(done) {
expect(x).to.equal('foo')
})
})
}
const wait = () => new Promise((resolve, reject) => {
setTimeout(() => resolve(), 10000)
})
wait()
.then(() => doTest('foo'))
/* results in
* 0 passing (4ms)
*/
describe
並且it
可能不是異步完成的。 如果要在測試執行前等待10秒,則應使用支持異步執行的before
塊。 你before
回調需要一個arg,這是一個done
回調。 在這個區塊內,您可以等待10秒,然后調用完成。 所有后續塊將等到done
從內調用before
塊。
describe('your tests', () => {
before(done => {
setTimeout(done, 10000)
})
it('should return foo', () => {
expect(x).to.equal('foo')
})
})
問題是您正在異步創建測試套件。 默認情況下,Mocha不允許異步創建套件。 我拿了你的代碼並進行了以下更改:
從傳遞給it
的回調中刪除了done
參數,因為它沒用。
我添加了一個在doTest
結束時run
的調用。
我使用--delay
選項調用Mocha: mocha --delay
。
--delay
選項將使Mocha等待構建測試套件。 您通過調用run()
表示已完成構建它。 以下是上述更改的完整代碼:
var chai = require('chai');
var cAP = require('chai-as-promised')
chai.use(cAP);
const expect = chai.expect;
const doTest = (x) => {
describe('main', function() {
it('should return foo', function() {
expect(x).to.equal('foo')
})
})
run();
}
const wait = () => new Promise((resolve, reject) => {
setTimeout(() => resolve(), 10000)
})
wait()
.then(() => doTest('foo'))
chai-as-promise可以使它更清潔一點
/**
* Modified by cool.blue on 26-Aug-16.
*/
'use strict';
var chai = require('chai');
var cAP = require('chai-as-promised');
chai.use(cAP);
const should = chai.should();
const wait5 = (something) => new Promise((resolve, reject) => {
setTimeout(() => resolve(something + 5), 5000)
});
const wait10 = (something) => new Promise((resolve, reject) => {
setTimeout(() => resolve(something + 10), 10000)
});
const doTest = (x, y, prep) => {
describe('main', function() {
this.timeout(15000);
it('should return the right number for ' + x + ' and ' + y, function() {
return prep(y).should.become(x)
})
})
};
[[17, 12, wait5], [15, 5, wait10], [15, 5, wait5]].forEach((listing) => doTest(...listing));
但是,要小心箭頭函數:它們以不同的方式處理語句和表達式。
it('should return the right number for ' + x + ' and ' + y,
() => prep(y).should.become(x)
)
完全不同於
it('should return the right number for ' + x + ' and ' + y, () => {
prep(y).should.become(x)
})
但完全相同
it('should return the right number for ' + x + ' and ' + y, () => {
return prep(y).should.become(x)
})
箭頭功能讓我很緊張,這就是為什么我更喜歡...
it('should return the right number for ' + x + ' and ' + y, function() {
return prep(y).should.become(x)
})
好吧,我找不到一個確切的答案,但我能找到一些對我真正需要的東西有用的東西,所以,這就是我的所作所為。
本質上,我可以將所有其他異步操作作為參數傳遞給測試函數,並且可以使用before塊的done()回調來確保它們在正確的時間執行。
const wait5 = (something) => new Promise((resolve, reject) => {
setTimeout(() => resolve(something + 5), 5000)
})
const wait10 = (something) => new Promise((resolve, reject) => {
setTimeout(() => resolve(something + 10), 10000)
})
const doTest = (x, y, prep) => {
describe('main', function() {
let z;
before(function (done) {
this.timeout(30000);
prep(y).then((val) => {
z = val;
done();
})
})
it('should return the right number for ' + x + ' and ' + y, () => {
expect(x).to.equal(z)
})
})
}
[[17, 12, wait5], [15, 5, wait10], [15, 5, wait5]].forEach((listing) => doTest(...listing))
/* Result:
main
√ should return the right number for 17 and 12
main
√ should return the right number for 15 and 5
main
1) should return the right number for 15 and 5
2 passing (20s)
1 failing
1) main should return the right number for 15 and 5:
AssertionError: expected 15 to equal 10
+ expected - actual
-15
+10
*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.