German Gracia
German Gracia

Reputation: 360

Angular Jasmine Unit Testing: Error: [$injector:unpr] Unknown provider: $confirmProvider

Hello I am writing my first angular test with Jasmine but I keep getting the error,i'm test controller y confirm not run test , i'm inject confirm in controller: Error: [$injector:unpr] Unknown provider: $confirmProvider my code is:

my Spec

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

my controller:

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

my app here inject confirmar:

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

};

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

Upvotes: 2

Views: 5539

Answers (1)

Diana R
Diana R

Reputation: 1174

You need to initialize all custom modules together with the module you will test:

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

Edit: Here is a simplified version of your plunkr that works. You have a few issues there. The first thing, you try to use $confirm which is not a standard angularjs directive, but has to be added separately, which means you have to inject it into your module, not only in your controller.

This is how it can be done:

1) add to your source code two dependencies:

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

You need ui-bootstrap because it is a dependency for angular-confirm directive.

These can be downloaded here and here.

2) inject into module angular.module('MyControllers', ['angular-confirm']) .controller('diagramaController', ['$scope','$confirm', function ($scope, $confirm) {

Upvotes: 3

Related Questions