acostela acostela - 1 year ago 455
AngularJS Question

uibModal provider unknown unitTest

I'm starting to learn how to do unitTesting with jasmine. I read a lot of in internet and SO but I can't solve my problem.

I have a directive which has a controller. That controller is using the service $uibModal to open a modal when I click an element. I'm trying to inject that service from my test but I can't. I read a lot of threads saying that I must pass an instance. I'm trying to do so but I can't. Please any help will be appreciated.

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

This is the test where I'm trying to mock this modal.


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


I'm getting the error

Unknown provider: $uibModalProvider <- $uibModal.

Can I inject this service in another way? What I'm doing wrong?

P.S: I have read this Testing AngularUI Bootstrap modal instance controller

Angular ui bootstrap $uibModalInstance breaks down unit tests

Mocking $modal in AngularJS unit tests

Answer Source

Finally I solved it. It was a silly error. I imported an additional module that I was missing in my tests. After that I could mock my service and use it without any problem like this.

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>');
    controller = $controller('directiveTreeController', {$scope: scope, $uibModal: uibModal});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download