5

I'm a newbie in testing Angular Apps with Jasmine, and I can't figure out the cause of this problem...

The controller

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

And this is the 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 output of the test

TypeError: Cannot read property 'disableSendBtn' of undefined

And if I test if the $scope variable is defined or not by

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

I get this error too

Expected undefined to be defined.

So what's the problem here?

HERE is the jsFiddle http://jsfiddle.net/LeoAref/bn1wxycs/


Edit

I used incorrect module here beforeEach(module('app'));

and I fixed that by using the correct module 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
LeoAref
  • 174
  • 1
  • 12

1 Answers1

0

Error: [ng:areq] Argument 'ticketCtrl' is not a function, got undefined

This error usually is thrown when trying to instantiate controller that is not defined in the module you initialize in the beforeEach function.

The code that is provided seems fine. The only thing that is missing is to check if karma successfully finds your file. Open your karma.conf.js file and make sure that the file where the controller is defined is listed in or picked up with the regex.

S.Klechkovski
  • 4,005
  • 16
  • 27