user266909
user266909

Reputation: 1863

Error: [$injector:itkn] Incorrect injection token! Expected service name as string, got undefined

This a continuing of my question from angualr karma jasmine unit test for a controller. I am getting

Error: [$injector:itkn] Incorrect injection token! Expected service name as string, got undefined. When I use [] in the controller constructor, I got

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- HomeCtrl

 $controller("HomeCtrl",[{
              $scope: scope
            }]);

Thank you for your help.

/// <reference path="../../_references.js" />

'use strict';

describe('Controllers: HomeCtrl', function() {
      var $controller, scope;
      beforeEach(module('myApp.controllers'));
      beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        $controller("HomeCtrl", {
          $scope: scope
        });
      }));

      it('should has title equals to My App', function() {
        expect(scope.title).toEqual('My App');
      });
}

The HomeController.js is like this:

'use strict';

angular.module('myApp.controllers')
   // Path: /
  .controller('HomeCtrl', ['$scope', '$location', '$window', /*'version'*/,
    function ($scope, $location, $window, version)
    {
      $scope.$root.title = 'AngularJS SPA | Home';
      //$scope.appVersion = version;
      $scope.title = 'My App';
    }]);

Upvotes: 1

Views: 4763

Answers (2)

Shahid Hussain Abbasi
Shahid Hussain Abbasi

Reputation: 2692

The actual problem is that I had an extra , in the controller. When I changed that, it worked. , function($scope, $timeout ....

Upvotes: 0

Michael
Michael

Reputation: 3104

.controller('HomeCtrl', ['$scope', '$location', '$window', /*'version',*/
    function ($scope, $location, $window)

You have to remove the comma after the comment - like this you have 2 following commas which resolves to an undefined value. You also have to remove version from the function or the argument don't match with signature.

Upvotes: 4

Related Questions