4
votes

Preface : I'm quite new to Angular and very new to unit-testing. Be gentle.

I'm trying to run unit tests on a controller for a bootstrap modal window.

When initiating the modal I am passing through an object called thisVersion like so :

 function showShareDialog(thisVersion){
        var modalInstance = $modal.open({
            templateUrl: 'app/shares/views/shares-dialogue.tpl.html',
            controller: 'SharesCtrl',
            resolve:{
                thisVersion:function(){
                    return thisVersion;
                }
            }
        });
        modalInstance.result.then(function (validated) {
            // .. more code
        });
    }    

As soon as the Shares Controller is instantiated I am calling a method on thisVersion

thisVersion.getList('shares').then(function(result){
    $scope.shares = result;
});

thisVersion is being passed in to the controller as a dependency, and that all works as expected.

The issue, however, is that I can't seem to inject it into my test suites. I keep getting this error

Error: [$injector:unpr] Unknown provider: thisVersionProvider <- thisVersion

This is (most of) my test suite :

var scope, controller, thisVersion;

  beforeEach(module('app'));
  beforeEach(inject(function ($controller, $rootScope, _thisVersion_) {

        scope = $rootScope.$new();       
        controller = $controller('SharesCtrl', {
            $scope: scope
        });
        thisVersion = _thisVersion_;

    }));

 it('should get a list of all shares',  function(){
     expect(thisVersion.getList).not.toBe('undefined');
 });

I know I am going to have to mock the call to the Api from thisVersion.getList() but for now I'm just concerned with getting the test suite to recognise thisVersion

1
Is thisVersion an Angular service? You just mention its an object.woolyninja

1 Answers

-1
votes

You need to inject thisVersion into the $controller, seeing as how it's a resolved value in $modal.open.

    var modalInstance = $modal.open({
        templateUrl: 'app/shares/views/shares-dialogue.tpl.html',
        controller: 'SharesCtrl',
        resolve:{
            // Resolved properties will be available for DI, into the controller.
            thisVersion:function(){ 
                return thisVersion;
            }
        }
    });

beforeEach(module('app'));
beforeEach(inject(function ($controller, $rootScope, _thisVersion_) {

  scope = $rootScope.$new();

  controller = $controller('SharesCtrl', {
    $scope: scope,
    thisVersion: _thisVersion_ /** inject the service _into_ the controller **/
  });
}));