2017-01-17 5 views
0

У меня есть угловой компонент, и я хочу проверить состояние после вызова метода асинхронизации. Как я могу это сделать? Мой метод doSomething не возвращает обещание!Тестирование метода асинхронной компоновки

angular.module('myModule').component('myComponent', { 
    template: '<p></p>', 
    controller: function($q) { 
    this.state = 1; 
    this.doSomething: function() { 
     var that = this; 
     setTimeout(function() { that.state = 2; }, 50); 
    } 
    } 
}); 

Тест

describe('Test', function() { 
    var ctrl = null; 

    beforeEach(module('myModule')); 
    beforeEach(inject(function(_$componentController_) { 
     ctrl = _$componentController_('myComponent', null, {}); 
    })); 

    it('should be 2', function() { 
     ctrl.doSomething(); 
     expect(ctrl.state).toBe(2); 
    }); 
}); 

ответ

0

Вам нужно будет ждать обновления в тесте, что-то вроде этого:

describe('Test', function() { 
    var ctrl = null; 

    beforeEach(module('myModule')); 
    beforeEach(inject(function(_$componentController_) { 
     ctrl = _$componentController_('myComponent', null, {}); 
    })); 

    it('should be 2', function(done) { 
     ctrl.doSomething(); 
     setTimeout(function() { 
     expect(ctrl.state).toBe(2); 
     done(); 
     }, 52); 
    }); 
}); 

надежду, что эта помощь.

Смежные вопросы