2017-01-17 3 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); 
    }); 
}); 

희망이 도움이.