0

我开始学习如何用茉莉花做单元测试。我在互联网上阅读了很多,但我无法解决我的问题。uibModal provider unknown unitTest

我有一个指令,它有一个控制器。当我点击一个元素时,该控制器正在使用服务$ uibModal打开一个模式。我试图从我的测试中注入该服务,但是我不能。我读了很多线索,说我必须通过一个实例。我试图这样做,但我不能。请任何帮助,将不胜感激。

.controller('myController', ['$scope', '$uibModal', function($scope, $uibModal){ 
    var self = this; 
    //OTHER CODE 
    self.openMyModal = function(dataInput) { 
     var modalInstance = $uibModal.open({ 
      animation: true, 
      bindToController: true, 
      templateUrl: 'app/myComponent/modals/component-modal.html', 
      controllerAs: 'componentModalCtrl', 
      controller: 'componentModalController', 
      windowClass: 'semi-modal semi-modal--large', 
      scope: $scope 
     }) 
    } 
    //OTHER CODE 
} 

这是我试图嘲笑这种模式的测试。

beforeEach(function(){ 
    angular.mock.module('templates'); 
    angular.mock.module('app.components.myComponent'); 

    angular.mock.inject(function($compile, $rootScope, $templateCache, $controller){ 
     scope = $rootScope; 
     modalInstance = { close: function(){}, dismiss: function(){}, open: function(){} 
    }; 
    //Initializing element and doing compile and digest 
    controller = $controller('myController', {$scope: scope, $uibModal: modalInstance}); 

}) 

,我发现了错误

未知提供商:$ uibModalProvider < - $ uibModal。

我可以用另一种方式注入这项服务吗?我做错了什么?

P.S:我已阅读本Testing AngularUI Bootstrap modal instance controller

Angular ui bootstrap $uibModalInstance breaks down unit tests

Mocking $modal in AngularJS unit tests

回答

0

最后我解决它。这是一个愚蠢的错误。我导入了一个我在测试中缺少的模块。之后,我可以嘲笑我的服务,并使用它,没有像这样的问题。

angular.mock.inject(function($compile, $rootScope, $templateCache, $controller, $uibModal){ 
    scope = $rootScope; 
    uibModal = $uibModal; 
    element = angular.element('<directive-tree input-tree=inputTree subsystem=subsystem></directive-tree>'); 
    $compile(element)(scope); 
    scope.$digest(); 
    controller = $controller('directiveTreeController', {$scope: scope, $uibModal: uibModal}); 
    }); 
1

试试这个:

beforeEach(module(function ($provide) { 
    $provide.service("$uibModal", function() { 
     // mock methods here 
    }); 
}));