Expected undefined should be defined when testing angular controller with Jasmine

I am new to testing Angular applications with Jasmine and I cannot figure out the cause of this problem ...

Controller

programsModule.controller('ticketCtrl', ['$scope', function ($scope) { $scope.disableSendBtn = true; }); 

And this is unit test

 'use strict'; describe('app', function () { // variables var $rootScope, $scope, $controller; beforeEach(module('supportModule')); describe('ticketCtrl', function () { beforeEach(inject(function (_$rootScope_, _$controller_) { $rootScope = _$rootScope_; $scope = $rootScope.$new(); $controller = _$controller_('ticketCtrl', { '$scope': $scope }); })); it('Should disable send btn', function () { expect($scope.disableSendBtn).toEqual(true); }); }); }); 

And this is the test result

 TypeError: Cannot read property 'disableSendBtn' of undefined 

And if I check if the variable $scope defined or not

 it('Should $scope be defined', function () { expect($scope).toBeDefined(); }); 

I get this error too

 Expected undefined to be defined. 

So what is the problem?

There is jsFiddle here http://jsfiddle.net/LeoAref/bn1wxycs/


Edit

I used the wrong module here beforeEach(module('app'));

and I fixed it using the correct beforeEach(module('supportModule'));

And I got another error:

 Error: [ng:areq] Argument 'ticketCtrl' is not a function, got undefined http://errors.angularjs.org/1.4.1/ng/areq?p0=ticketCtrl&p1=not%20a%20function%2C%20got%20undefined 
+5
source share
1 answer

Error: [ng: areq] The argument 'ticketCtrl' is not a function, received undefined

This error usually occurs when trying to create an instance of a controller that is not defined in the module that you initialize in the beforeEach function.

The code that is provided seems fine. The only thing missing is to check if karma successfully finds your file. Open the karma.conf.js file and make sure that the file in which the controller is installed is specified or added to the regular expression.

0
source

All Articles