【问题标题】:Jasmine Async Error : Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVALJasmine 异步错误:超时 - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内未调用异步回调
【发布时间】:2017-07-24 01:55:57
【问题描述】:

Jasmine 新手,我正在测试一个 async 函数。它显示一个错误,说 Error: Timeout - Async callback was not invoked within timeout specified by 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();
     });

【问题讨论】:

    标签: javascript unit-testing jasmine


    【解决方案1】:

    来源

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-21
      • 2018-11-21
      • 2015-05-26
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      相关资源
      最近更新 更多