【问题标题】:uibModal provider unknown unitTestuibModal 提供者未知 unitTest
【发布时间】:2016-07-14 13:37:31
【问题描述】:

我开始学习如何使用 jasmine 进行单元测试。我在互联网上阅读了很多内容,但我无法解决我的问题。

我有一个带有控制器的指令。当我单击一个元素时,该控制器正在使用服务 $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

我可以通过其他方式注入此服务吗?我做错了什么?

P.S:我已经读过这个Testing AngularUI Bootstrap modal instance controller

Angular ui bootstrap $uibModalInstance breaks down unit tests

Mocking $modal in AngularJS unit tests

【问题讨论】:

    标签: javascript angularjs twitter-bootstrap unit-testing jasmine


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

      终于解决了。这是一个愚蠢的错误。我导入了一个我在测试中缺少的附加模块。之后,我可以模拟我的服务并毫无问题地使用它。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-27
        • 2016-11-29
        • 2018-09-28
        • 2014-12-01
        • 1970-01-01
        相关资源
        最近更新 更多