0

I have set up my Angularjs application for unit testing with Karma and Jasmin. but when I try to run my test cases with karma start I am getting the error The controller with the name Myctrl is not registered.

My application structure is as follows

project-folder
  - app
    - components
      - controllers
        - account
          - signInController.js
          - SignUpController.js
  - app.modules.js
  - app.routes.js
  - test

This is my app.modules.js

let app = angular.module( 'app', [
  'app.config', 'templates', 'ngAnimate', 'ngAria', 'ngCookies', 'ngMessages', 'ngResource', 'ngSanitize',
  'ngTouch', 'ui.router', 'ui.bootstrap', 'ui.utils', 'ui.load', 'ui.jq', 'oc.lazyLoad','angular-cache',
  'ngToast', 'ngFileUpload', 'ngFileSaver', 'angularMoment', 'angulartics', 'angulartics.google.analytics',
  'ngMessages', 'ng.httpLoader'
]);

And the karma.config.js file

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'browserify'],

    // list of files / patterns to load in the browser
    files: [
      'dist/libs/jquery/jquery.js',
      'dist/libs/angular/angular.js',
      'dist/libs/angular-ui-router/angular-ui-router.js',
      'dist/libs/angular-sanitize/angular-sanitize.js',
      'dist/libs/angular-mocks/angular-mocks.js',
      'dist/libs/angular-bootstrap/ui-bootstrap-tpls.js',
      'app/app.modules.js',
      'app/app.routes.js',
      'app/config.lazyload.js',
      'app/components/controllers/account/*.js',
      'test/**/*spec.js',
      ],
      . 
      .
describe('Myctrl Test', function() {

  describe('Myctrl', function() {
    var vm;

    beforeEach(inject(function( ) {
      angular.module('app')
    }));

    beforeEach(inject(function(_$rootScope_, $controller) {
      var scope = _$rootScope_.$new();
      vm = $controller('Myctrl', {$scope: scope});
    }));

    it('test controller', function() {
      expect(vm.title).toBe(null);
    });

  });

});
app.controller( 'Myctrl', ['$scope', '$state', 'backendApi', 'ngToast', 'access_token', 'FileUploader', 'keys',
function( $scope, $state, backendApi, ngToast, access_token, FileUploader, keys ) {

  $scope.title = "Hello";


}])
Ankur Pohekar
  • 129
  • 1
  • 12

1 Answers1

0

Before each test you have to register a module.

The problem is that you tried native AngularJS angular.module('app'), while you have to use module('app') which is an alias for angular.mock.module

This function registers a module configuration code. It collects the configuration information which will be used when the injector is created by inject.

Change your code to

beforeEach(function( ) {
  module('app')
});

and make sure Myctrl belongs to the app module.

Consider the following article Testing a Controller section.

FYI: angular.module('app') is only required for referencing a module inside angular application, but this function is not actually including module.

Majesty
  • 2,097
  • 5
  • 24
  • 55