Jasmine单元测试等待异步调用

时间:2017-01-17 15:47:42

标签: angularjs unit-testing asynchronous jasmine karma-runner

如何在运行expect之前让Jasmine等待异步函数完成?

我想在我的控制器中测试scope.init()。此函数包含3个与此类似的异步调用

$scope.init = function() {

   //populate parameterList 

    myService.getDropdowns(parameterList).then(function(response) {
        if (response && response.data) {
            $scope.dropdown1 = response.data;
        }

    }, function(errorResponse) {
        $log.error(errorResponse);
    });

  //populate parameterList 

    myService.getDropdowns(parameterList).then(function(response) {
        if (response && response.data) {
            $scope.dropdown2 = response.data;
        }

    }, function(errorResponse) {
        $log.error(errorResponse);
    });

};

如何在运行我的期望之前等待dropdown1,dropdown2,dropdown3填充?我试图测试每个下拉列表是否已成功填充。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

 describe('your test', function(){

    beforeEach(function(done){
        // You can call any async task, when done() is called the test will begin
        setTimeout(() => {done();}, 100); 
    });

    it('Column Definitions created', function(){
        expect($scope.a).toBe(3);
    });
});
相关问题