【问题标题】:Jasmine unit test wait for asynchronous callsJasmine 单元测试等待异步调用
【发布时间】:2017-06-01 18:27:40
【问题描述】:

如何让 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 被填充?我正在尝试测试每个下拉列表是否已成功填充。

【问题讨论】:

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


    【解决方案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);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2018-02-13
      相关资源
      最近更新 更多