AngularJS error Karma Jasmine: cannot read Undefined property

This is my first attempt to use Karma / Jasmine. I want to use it to test an AngularJS application.

This is a simple Hello World test to make sure everything works, but it doesn’t.

I am trying to test a controller called InboxController. I defined a scope variable: $ scope.greeting = 'Hello World'.

I am trying to verify this in a separate file called controller-test.js.

This is a test:

'use strict';

describe ("Controller tests", function () {

describe('InboxController', function () {
    var scope;

    beforeEach(angular.mock.module('myApp'));

    beforeEach(angular.mock.inject(function($rootScope, $controller){
        scope = $rootScope.$new();
        $controller('InboxController', {$scope: scope});

    }));

    it('should return Hello World', function () {
        expect(scope.greeting).toBe('Hello World');
    });
});

});

I get an error message:

It is not possible to read the undefined property greeting, which is part of scope.greeting in the test code.

Karma configuration:

files : [

            'bower_components/angular/angular.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'bower_components/angular-resource/angular-resource.js',
            'app.js',
            'js/*.js',
            'test/*.js'
             ],

InboxController:

app.controller('InboxController', function($rootScope, $scope,
    $location, InboxService) {

$scope.greeting = 'Hello World';

$rootScope.loggedIn = true;

// Load Inbox
$scope.loadInbox = function() {

    $scope.emails = InboxService.getMessages().success(function(jsonData) {

        $scope.emails = jsonData;
    });
}

// Delete email
$scope.delete = function (id) {
    InboxService.delete(id).success(function() {

        $scope.loadInbox();
    });
};

$scope.loadInbox();

});

+4
1

. angular :

files : [

            'bower_components/angular/angular.js',
            'bower_components/angular-route/angular-route.js',
            'bower_components/angular-sanitize/angular-sanitize.js',
            'bower_components/angular-cookies/angular-cookies.js',
            'bower_components/angular-bootstrap/ui-bootstrap.js',
            'bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'bower_components/angular-resource/angular-resource.js',
            'app.js',
            'js/*.js',
            'test/*.js'
             ],

, :

bower_components/angular/angular.js
+2

Source: https://habr.com/ru/post/1584196/


All Articles