茉莉花单元测试等待异步调用

Jasmine unit test wait for asynchronous calls

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

我想在我的控制器中测试 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 在 运行 我的预期之前被填充?我正在尝试测试是否已成功填充每个下拉列表。

你可以这样做:

 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);
    });
});