Angular Jasmine单元测试:错误:[$ injector:unpr]未知提供者:$ confirmProvider

时间:2015-09-21 21:05:13

标签: jasmine

您好我正在使用Jasmine编写我的第一个角度测试但我一直收到错误,我是测试控制器y确认未运行测试,我在控制器中注入确认:错误:[$ injector:unpr]未知提供者: $ confirmProvider 我的代码是:

我的规格

describe('Controller : diagramaController', function () {
    var scope, rootScope, controller, $confirm, UserCtrl;

    beforeEach(function () {
        module('Mycontrollers');
    });

    beforeEach(inject(function ($rootScope, $controller, _$confirm_, $routeParams) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        scope = $rootScope.$new();
        $confirm = _$confirm_;
        UserCtrl = $controller('diagramaController', { $scope: scope, $confirm: confirm });
    }));

    it('Deberia agregar  y sumar uno', function () {
        //var $scope = {};
        //var controller = $controller('actividadController', { $scope: $scope });
        var contador = 0;
        expect(contadoractual).toBe(contadorGenerado - 1);
    });
});

我的控制员:

    (function () {
    angular.module('Mycontrollers', [])
    .controller('diagramaController',['$scope', '$http', '$confirm', '$filter', '$timeout', '$routeParams', '$modal', '$rootScope',
        function ($scope, $http, $confirm, $filter, $timeout, $routeParams,$modal, $rootScope)
     {
      $scope.contador = 1;
      })();

我的应用程序在这里注入确认:

(function () {
    var app = angular.module('myApp', [
        'angucomplete-alt',
        'ngDraggable',
         'Mycontrollers',
         'ngRoute',
        'ui.bootstrap',
        'Confirmar'
    ])
  })();

};

确认js

angular.module("Confirmar", [])
.controller('ConfirmModalController', function ($scope, $modalInstance, data) {
    $scope.data = angular.copy(data);
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
})
.value('$confirmModalDefaults', {
    template: '<div class="modal-header"><h3 class="modal-title">Confirmar</h3></div><div class="modal-body">{{data.text}}</div><div class="modal-footer"><button class="btn btn-primary" ng-click="ok()">Aceptar</button><button class="btn btn-primary" ng-click="cancel()">Cancelar</button></div>',
    controller: 'ConfirmModalController'
})
.factory('$confirm', function ($modal, $confirmModalDefaults) {
    return function (data, settings) {
        settings = angular.extend($confirmModalDefaults, (settings || {}));
        data = data || {};

        if ('templateUrl' in settings && 'template' in settings) {
            delete settings.template;
        }
        settings.resolve = { data: function () { return data; } };
        return $modal.open(settings).result;
    };
})

1 个答案:

答案 0 :(得分:3)

您需要将所有自定义模块与要测试的模块一起初始化:

beforeEach(function () {
    module('Mycontrollers');
    module('Confirmar');
    module('myApp');
});

编辑: Here是您的plunkr的简化版本。你有几个问题。首先,您尝试使用$ confirm,它不是标准的angularjs指令,但必须单独添加,这意味着您必须将其注入模块,而不仅仅是在控制器中。

这是如何做到的:

1)在源代码中添加两个依赖项:

<script src="ui-bootstrap-tpls-0.13.0.min.js"></script>
<script src="angular-confirm.js"></script>

你需要ui-bootstrap,因为它是angular-confirm指令的依赖项。

可以下载herehere

2)注入模块      angular.module(&#39; MyControllers&#39;,[&#39; angular-confirm&#39;])     .controller(&#39; diagramaController&#39;,[&#39; $ scope&#39;,&#39; $ confirm&#39;,         函数($ scope,$ confirm){