The controller is not a function, has not been defined in jasmine tests with angular (route - $ stateProvider)

advertisements

i am using angle theme with Angular for my project and i am doing unit testing on this using jasmine framework. But when i run the test cases, it gives error of "Argument 'controller' is not a function, got undefined". here is my test.js file....

describe('registrationController',function(){
beforeEach(module('appTesting'));
var $controller;
beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
}));
it('Check Working Or Not', function() {
    var $scope = {};
    var controller = $controller('registrationController', { $scope: $scope });
    $scope.password = 'passwordlength';
    $scope.grade();
    expect($scope.strength).toEqual('strong');
});

});

Error -

Error: [ng:areq] Argument 'registrationController' is not a function, got undefined http://errors.angularjs.org/1.3.10/ng/areq?p0=registrationController&p1=not%20a%20function%2C%20got%20undefined

No i am using karma to run jasmine.

I am defining my module here

var App = angular.module('appTesting', ['ngRoute', 'ngAnimate', 'ngStorage', 'ngCookies', 'pascalprecht.translate', 'ui.bootstrap', 'ui.router', 'oc.lazyLoad', 'cfp.loadingBar', 'ngSanitize', 'ngResource'])
      .run(["$rootScope", "$state", "$stateParams",  '$window', '$templateCache', function ($rootScope, $state, $stateParams, $window, $templateCache, $location) {
          // Set reference to access them from any scope
          $rootScope.$state = $state;
          $rootScope.$stateParams = $stateParams;
          $rootScope.$storage = $window.localStorage;
        }
      ]);

App.config(['$stateProvider','$urlRouterProvider', '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', '$ocLazyLoadProvider', 'APP_REQUIRES', 'RouteHelpersProvider',
function ($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide, $ocLazyLoadProvider, appRequires, helper) {

 'use strict';
App.controller = $controllerProvider.register;
App.directive  = $compileProvider.directive;
 App.filter     = $filterProvider.register;

App.factory    = $provide.factory;

App.service    = $provide.service;
App.constant   = $provide.constant;
 App.value      = $provide.value;
 $stateProvider
.state('app.registrationState', {
    url: '/registration',
    title: 'Ragistration Page',
    templateUrl: helper.basepath('registrationPage.html'),
    resolve: helper.resolveFor('registrationController','angularFileUpload')
})
}])
.controller('NullController', function() {});
App.constant('APP_REQUIRES', {
scripts:
{
  'registrationController' :['app/js/registrationcontroller.js'],
}
});


Angular

Can the controller be found?

  • Do you have a controller named registrationController?
  • Is it in a module named appTesting?

You should have the following code somewhere in your app:

angular.module("appTesting").controller("registrationController", ...);

Karma

If you are using Karma-jasmine:

  • Did you include the file where you define the controller (registration-controller.js) in your karma.conf.js?

karma.conf.js

module.exports = function(config) {config.set({
  files : [
    'src/registration-controller.js',
    ...
    ],
  ...
});};

Jasmine

If you're not using Karma to run Jasmine, I'm assuming you're using the Jasmine test runner (HTML file). In that case:

  • Did you include the controller in the HTML file?

You need to include the controller's script:

<head>
  ...
  <script type="text/javascript" src="src/registration-controller.js"></script>
  ...
</head>