TypeError: undefined is not a constructor (evaluating 'angular.controller('myView')')

2k Views Asked by At

I'm getting the below error while doing karma/jasmine unit testing for both the test cases.I tried by modifying the controller by adding angular.controller in the spec file even then it is not working.Is there any way to fix?

 TypeError: undefined is not a constructor (evaluating 'angular.controller('myView')') 

myView.spec.js

// myView.spec.js
(function(){
describe('controller: myView', function(){


     var module,myView,$q, $rootScope, $scope, uiGridConstants, overviewService, commonService, $timeout;
    beforeEach(function() {
        module = angular.module('app.myView');
        controller= angular.controller('myView')
    });

    beforeEach(inject(function ($controller, _$q_, _$rootScope_, _$timeout_) {

        $q= _$q_;
        $rootScope = _$rootScope_;
         $timeout= _$timeout_;

        myView= $controller('myView', {
            $q : _$q_,
            $rootScope :  _$rootScope_,
             $timeout:  _$timeout_
        });


    }));

    describe("myViewto be defined", function() {
        it("should be created successfully", function () {
            expect(controller).toBeDefined();
        });

        it("overview should be defined", function () {
            expect(myView()).toBeDefined();
        });

    });

});

})();

and myView.js

(function() {
    'use strict';

    angular
        .module('app.myView')
        .controller('myView', myView);

    function myView($q, $rootScope, $scope, uiGridConstants, myViewService, commonService, $timeout) {
        var vm = this;
        vm.callFeedback = function () { };
})();
2

There are 2 best solutions below

0
Prasanna On

Sharing following code

// myView.spec.js
(function(){
describe('myView', function(){
  var $controller, myView;

   //we use angular-mocks to specify which modules we'll need within this  
   //test file. 
   beforeEach(angular.mock.module('app.myView'));

   // Inject the $controller service to create instances of the controller 
   //(myView) we want to test
   beforeEach(inject(function(_$controller_) {
      $controller = _$controller_;
      myView = $controller('myView', {});
    }));

   // Verify our controller exists
   it('should be defined', function() {
      expect(myView).toBeDefined();
   });
  });

})();

We set _$controller_to the $controller variable we created and then create an instance of our controller by calling $controller('myView', {}). The first argument is the name of the controller we want to test and the second argument is an object of the dependencies for our controller.

0
Koji D'infinte On

You should pass the injected parameters to your controller as shown:

(function() {
'use strict';

angular
    .module('app.myView')
    .controller($q,$rootScope,$scope,uiGridConstants,'myView', myView);

function myView($q, $rootScope, $scope, uiGridConstants, myViewService, commonService, $timeout) {
    var vm = this;
    vm.callFeedback = function () { };

})();

Also make sure that your module has all the necesary dependences in the angular.module('app.myView',['uiGridConstants', ...'etc']);