2017-03-03 1 views
0

재스민에 새로 추가되어 async 함수를 테스트하고 있습니다. 그것의 오류를 보여주는 Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 내가 여기 뭔가를 놓치고 있다면 도와주세요. 시험에재스민 비동기 오류 : 시간 초과 - jasmine.DEFAULT_TIMEOUT_INTERVAL에 지정된 제한 시간 내에 비동기 콜백이 호출되지 않았습니다.

기능 :

function AdressBook(){ 
    this.contacts = []; 
    this.initialComplete = false; 
} 
AdressBook.prototype.initialContact = function(name){ 
    var self = this; 
    fetch('ex.json').then(function(){ 
     self.initialComplete = true; 
     console.log('do something'); 
    }); 
} 

테스트 사양은 다음과 같다

var addressBook = new AdressBook(); 
    beforeEach(function(done){ 
     addressBook.initialContact(function(){ 
      done(); 
     }); 
    }); 
    it('should get the init contacts',function(done){ 
      expect(addressBook.initialComplete).toBe(true); 
      done(); 
    }); 

답변

0

소스

function AddressBook() { 
    this.contacts = []; 
    this.initialComplete = false; 
} 

AddressBook.prototype.initialContact = function(name) { 
    var self = this; 
    // 1) return the promise from fetch to the caller 
    return fetch('ex.json').then(function() { 
      self.initialComplete = true; 
      console.log('do something'); 
     } 
     /*, function (err) { 
       ...remember to handle cases when the request fails 
      }*/ 
    ); 
} 

테스트

describe('AddressBook', function() { 
    var addressBook = new AddressBook(); 
    beforeEach(function(done) { 
     // 2) use .then() to wait for the promise to resolve 
     addressBook.initialContact().then(function() { 
      done(); 
     }); 
    }); 
    // done() is not needed in your it() 
    it('should get the init contacts', function() { 
     expect(addressBook.initialComplete).toBe(true); 
    }); 
}); 
관련 문제