2016-04-09 12 views
2

Jasmine gibt einen Fehler für meine dritte Spezifikation aus. Fehler: Timeout - Async-Rückruf wurde nicht innerhalb des von jasmine.DEFAULT_TIMEOUT_INTERVAL angegebenen Zeitlimits aufgerufen.Jasmine löst einen Fehler aus - Fehler: Timeout - Async-Rückruf wurde nicht aufgerufen

hier ist meine Spec-Datei

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(); 
    }); 

}); 

Hier meine src-Datei ist

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); 
} 

ich das Problem nicht bekommen .. Bitte nehmen Sie sich einen Blick .. Vielen Dank im Voraus.

Antwort

3

Problem ist mit getInitialContacts Funktion. Es ruft nie den bereitgestellten Rückruf auf, sondern gibt es nur zurück.

AddressBook.prototype.getInitialContacts = function(cb){ 

     var self = this; 

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

    } 

Es sollte Rückruf aufrufen geändert werden, wenn Asynchron-Vorgang beendet ist:

AddressBook.prototype.getInitialContacts = function(cb){ 

    var self = this; 

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

} 
+0

Dank paaren .... –

Verwandte Themen