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

は、このヘルプを願っています。