1
votes

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.

1
Does Karma include the files that define your starter.controllers module as well as the TemplateCtrl controller?Phil
The app fails on bootstrap (inject(...)), thus scope stays undefined. Phantomjs is known for swallowing errors, if there are no other errors shown in the console, try to change it to Chrome.Estus Flask
Seems to work just fine over here ~ plnkr.co/edit/NwcVhYN7fL16R1TzBFOH?p=previewPhil
Thank you for your responses, as soon as I get home from work I will try to change from Phantomjs to Chrome and will also check that Karma includes my files where starter.controllers are defined. Once I find out if those make my troubles go away, I'll reply. Thank you, again.Colin
@Phil Solved it for me: "Does Karma include the files that define your starter.controllers module as well as the TemplateCtrl controller?" I feel like an idiot... Thank you so so so much! Could you please post it as the answer?Colin

1 Answers

0
votes

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!