gpt4 book ai didi

javascript - Jasmine 抛出一个错误 - 错误 : Timeout - Async callback was not invoked

转载 作者:行者123 更新时间:2023-11-29 19:14:24 25 4
gpt4 key购买 nike

Jasmine 为我的第三个规范抛出一个错误。错误:超时 - 未在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内调用异步回调。

这是我的规范文件

describe('Address Book', function(){

var addressBook,
thisContact;

beforeEach(function(){
addressBook = new AddressBook(),
thisContact = new Contact();
});

it('Should be able to add a contact', function(){
addressBook.addContact(thisContact);
expect(addressBook.getContact(0)).toBe(thisContact);
});

it('Should be able to delete a contact', function(){
addressBook.addContact(thisContact);
addressBook.delContact(1);
expect(addressBook.getContact(1)).not.toBeDefined();
});

});

describe('Async Address Book', function(){

var addressBook = new AddressBook();

beforeEach(function(done){
addressBook.getInitialContacts(function(){
done();
});
});

it('Should be able to add a contact', function(done){
expect(addressBook.initialContacts).toBe(true);
done();
});

});

这是我的源文件

function AddressBook(){
this.contacts = [];
this.initialContacts = false;
}
AddressBook.prototype.getInitialContacts = function(cb){

var self = this;

setTimeout(function(){
self.initialContacts = true;
if (cb) {
return cb;
}
}, 3);

}

AddressBook.prototype.addContact = function(contact){
this.contacts.push(contact);
}

AddressBook.prototype.getContact = function(index){
return this.contacts[index];
}

AddressBook.prototype.delContact = function(index){
this.contacts.splice(index,1);
}

我没明白这个问题。请看一下。提前致谢。

最佳答案

getInitialContacts 函数有问题。它从不调用提供的回调,它只是返回它。

    AddressBook.prototype.getInitialContacts = function(cb){

var self = this;

setTimeout(function(){
self.initialContacts = true;
if (cb) {
return cb;
}
}, 3);

}

应该改为在异步操作完成时调用回调:

AddressBook.prototype.getInitialContacts = function(cb){

var self = this;

setTimeout(function(){
self.initialContacts = true;
if (typeof cb === 'function') {
cb();
}
}, 3);

}

关于javascript - Jasmine 抛出一个错误 - 错误 : Timeout - Async callback was not invoked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com