下面是一个示例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