0

關於測試角模態的SO有很多問題,但他們似乎都是模仿模態實例。我實際上想通過實現來調用,特別是打開的promise回調。這是我的模塊:測試角模態的開放諾言

angular.module('MyApp') 
.directive('widgetContainer', function() { 
    return { 
    templateUrl: '/static/templates/container.html', 
    controller: 'ContainerCtrl' 
    }; 
}) 
.controller('ContainerCtrl', ['$scope', '$modal', function($scope, $modal) { 

    function editWidget(widget) { 
    var modalInstance = $modal.open({ 
     templateUrl: '/static/templates//modal.html', 
     controller: 'ModalInstanceCtrl', 
     scope: $scope, 
     size: 'lg', 
     backdrop: 'static' 
    }); 
    modalInstance.opened.then(function() { 
     $scope.widgetCopy = editWidgetInit(widget); 
    }); 
    modalInstance.result 
     .then(function() { 
     // some result 
     }); 
    return modalInstance; 
    } 

    function editWidgetInit(widget) { 
    widgetCopy = setSelectedChart(widget); 
    // lots of other setup tasks 
    return widgetCopy; 
    } 

}]); 

這是測試的樣子。

describe('on edit widget', function() { 
    it('should setup selectedChart from widget', function() { 
    var widget = {widget: {indicators: [{name: 'indicator'}]}}; 
    var modalInstance = scope.editWidget(widget); 
    rootScope.$digest(); 
    expect(scope.selectedChart).toBe('pie'); 
    }); 
}); 

這將打開模式實例,但打開的承諾永遠不會履行。它通常應該在創建新模式實例時觸發。

我可以用一個真正的電話測試這個而不嘲笑它,或者有另一個更好的方法來測試它嗎?

回答

0

在你的測試中,你可以自己解決opened的承諾。

describe('on edit widget', function() { 
    it('should setup selectedChart from widget', function() { 
    var widget = {widget: {indicators: [{name: 'indicator'}]}}; 
    var modalInstance = scope.editWidget(widget); 

    modalInstance.opened.resolve(); 

    rootScope.$digest(); 
    expect(scope.selectedChart).toBe('pie'); 
    }); 
});