下面是一個示例describe
,您可以運行並查看會發生什麼。我不得不提到我不使用量角器,因此可能會對其特定功能進行一些額外的考慮。
describe('Done functionality', function(){
var echoInOneSecond = function(value){
console.log('creating promise for ', value);
return new Promise(function(resolve, reject){
console.log('resolving with ', value);
resolve(value);
});
};
it('#1 this will untruly PASS', function(){
var p = echoInOneSecond('value #1');
p.then(function(value){
console.log('#1 expecting...and value is ', value);
expect(value).toBe('value #1');
});
});
it('#2 this will NOT FAIL', function(){
var p = echoInOneSecond('value #2');
p.then(function(value){
console.log('#2 expecting... and value is ', value);
expect(value).not.toBe('value #2');
});
});
it('3 = will truly FAIl', function(done){
var p = echoInOneSecond('value #3');
p.then(function(value){
console.log('#3 expecting... and value is ', value);
expect(value).not.toBe('value #3');
done();
});
});
it('4 = this will truly PASS', function(done){
var p = echoInOneSecond('value #4');
p.then(function(value){
console.log('#4 expecting... and value is ', value);
expect(value).toBe('value #4');
done();
});
});
});
運行測試時,你會注意到的順序:第一承諾#1,#2,#3將被創建並逐一解決。請注意,期望#1和#2將不會運行,因爲承諾是異步解決的。
然後,因爲#3測試使用done
,在創建#3諾言後,評估所有先前承諾的功能then
s:您會看到'#1期待...'和'#2期待...... ',但茉莉花不會在意這一點,因爲測試#1和#2已經完成,並完成了有關他們的一切。只有在第三種期望得以實現之後,它纔會真正失敗,因爲茉莉花會照顧在done()
之前發生的所有事情。
然後你可以看#4測試正常流量 - 創造承諾,解決,期望,茉莉花考慮的一切,所以期望會真正通過。
這只是一個回調,表示完成這個規範。你會留在這個規範中,直到done()被調用。 – Gunderson
@Gunderson--你能告訴我更多關於它的信息嗎? – rafalf