AngularJS Karma Jasmine错误:无法读取未定义的属性

这是我第一次尝试使用Karma / Jasmine.我想用它来测试AngularJS应用程序.

这只是一个简单的Hello World测试,以确保工作正常,但事实并非如此.

我正在尝试测试一个名为InboxController的控制器.我已经定义了一个范围变量:$scope.greeting =’Hello World’.

我试图在一个名为controllers-test.js的单独文件中测试它.

这是测试:

'use strict';

describe(‘Controllers 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');
    });
});

});

我一直收到错误:

无法读取未定义的属性“问候”,这与测试代码中的scope.greeting有关.

Karma配置:

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();

});

最佳答案 自己解决了这个问题.我需要引用karma配置文件中的所有角度依赖项:

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
点赞