0

Я, наконец, работаю над обучением, как тестировать, используя более раннее приложение angularjs, которое я написал. У меня есть несколько модалов в моем контроллере, и я не могу понять, насколько я убежден, что код в «modalInstance.result.then» запущен и протестирован против него.Тестирование modalInstance.result.then

Я искал Google и SO и нашел примеры людей, проверяющих свои модальности, но до сих пор они, похоже, требуют тестирования самого модального контроллера.

Как я могу получить это обещание (modalInstance.result.then), чтобы решить? Я попробовал запустить $ modal.close(), но это не приводит к ошибке. Я пробовал издеваться над modalInstance и $ modal несколькими способами, используя шпионы жасмина и т. Д. Мое невежество, когда дело доходит до тестирования, держит меня. Любая помощь будет вызвана.

Вот мои controller.js:

(function() { 
    var comment = angular.module('APP.comment', ['APP.user']); 

    var commentController = function($scope, $modal) { 

     var self = this; 

     self.addComment = function(newComment) { 

     var modalInstance = $modal.open({ 
      templateUrl: 'views/commentModal.html', 
      backdrop: 'static', 
      windowClass: 'modal', 
      controller: 'commentModalController', 
      controllerAs: 'commentCtrl', 
      resolve: { 
       newComment: function() { 
        return newComment; 
       } 
      } 
     }); 

     modalInstance.result.then(function(data) { 
      // How do I test that the function or branches here 
      // were run? 
      if (data.length === 2) { 
       // do this thing 
      } else { 
       // do this other thing 
      } 
     }); 
     }; 
    }; 

    commentController.$inject = ['$scope', '$modal']; 
    comment.controller('commentController', commentController); 
}()); 

Здесь вы тест, который я до сих пор:

describe('Unit: commentController', function() { 

    var $rootScope, 
     $scope, 
     $controller, 
     $modal; 

    beforeEach(module('APP.comment')); 

    beforeEach(inject(function(_$rootScope_, _$controller_, _$modal_) { 

     $modal = _$modal_; 

     $rootScope = _$rootScope_; 
     $scope = $rootScope.$new(); 

     $controller = _$controller_('commentController as commentCtrl', { 
      $scope: $scope, 
      $modal: $modal, 
     }); 

    })); 

    it('should have controller defined', function() { 
     expect($scope.qaCtrl).toBeDefined(); 
    }); 

    it('should have method defined', function() { 
     expect($scope.qaCtrl.addComment).toBeDefined(); 
    }); 

    describe('$scope.commentCtrl.addComment', function() { 
     it('should open modal', function() { 
      $scope.commentCtrl.addComment(); 
     }); 
    }); 
}); 

У меня есть plnkr здесь:
http://plnkr.co/edit/YtYVPReH9yysZXPjbsC0?p=preview

ответ

3
it('should open modal', inject(function($q) { 
    var fakeResult = { 
    result: $q.when([]) 
    }; 
    spyOn($modal, 'open').and.returnValue(fakeResult); 

    $scope.commentCtrl.addComment(); 

    $scope.$apply(); 

    // now check that the right thing has been done, given the empty array returned 
})); 
+0

Это сделал трюк, спасибо за ответ! – Woody2143

Смежные вопросы