Colin
Colin

Reputation: 168

Unit Testing a Controller Ionic Framework Karma

I've been beating my head against the wall trying to figure out why these tests aren't working using Karma and Jasmine to test an Ionic Controller. I'm new to ionic, karma unit testing, angularjs, and just about everything else here. Can anyone identify the reason these tests are failing?

Here's my controller:

    angular.module('starter.controllers')
    .controller('TemplateCtrl', function($scope) {
        $scope.text = 'Hello Template';
    });

Here's my test:

    describe('Template Controller', function(){
        var scope;
        beforeEach(module('starter.controllers'));
        beforeEach(inject(function($rootScope, $controller) {   
            scope = $rootScope.$new();                          
            $controller('TemplateCtrl', {$scope: scope});       
        }));

        // tests start here

        it('should have scope defined', function() {
           expect(scope).toBeDefined();
        });

        it('should have text set to Hello Template', function(){
             expect(scope.text).toEqual('Hello Template');
        });
    });

Test results:

PhantomJS 2.1.1 (Linux 0.0.0) Template Controller should have scope defined FAILED Expected undefined to be defined.

PhantomJS 2.1.1 (Linux 0.0.0) Template Controller should have text set to Hello Template FAILED TypeError: undefined is not an object (evaluating 'scope.text') in controller-tests/template.tests.js (line 23)

Thank you for your time.

Upvotes: 1

Views: 355

Answers (1)

Colin
Colin

Reputation: 168

Phil Identified the problem, without me even posting my karma config file:

Does Karma include the files that define your starter.controllers module as well as the TemplateCtrl controller?

Simply adding the path to the karma config file fixed my issue. Thank you Phil!

Upvotes: 0

Related Questions