Karma + Angular 未定义的错误

Karma + Angular undefined error

我刚开始 angular 使用 karma 和 jasmine 进行测试。我写了两个基本测试。两个测试之一通过,但另一个失败。我似乎无法调试它。我一直在到处寻找,它应该根据各种教程工作。 $scope 变量应该可用。我收到以下错误(以及大量文本,请参阅 ):

           at C:/totalflatline-2.0/public/lib/angular/angular.js:4362
           at forEach (C:/totalflatline-2.0/public/lib/angular/angular.js:336)
           at loadModules (C:/totalflatline-2.0/public/lib/angular/angular.js:4364)
           at createInjector (C:/totalflatline-2.0/public/lib/angular/angular.js:4248)
           at workFn (C:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2409)
           at C:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2392
           at C:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js:
5
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171
           at http://localhost:9876/karma.js:210
           at http://localhost:9876/context.html:83
       TypeError: 'undefined' is not an object (evaluating '$scope.test')
           at C:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js:
9
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171
           at http://localhost:9876/karma.js:210
           at http://localhost:9876/context.html:83
hantomJS 1.9.8 (Windows 7): Executed 2 of 2 (1 FAILED) (0.402 secs / 0.025 secs)

两个测试如下。第一个通过,另一个给出上述未定义的错误。

通过的那个:

describe('Testing MEAN Main Module', function() {
    var mainModule;
    beforeEach(function() {
        mainModule = angular.module('mean');
    });
    it('Should be registered', function() {
        expect(mainModule).toBeDefined();
        console.log('Success!');
    });
});

失败者:

describe('Testing the Shared Stats controller', function(){
    var SharedController,$scope;

    beforeEach(function(){
        // load the module you're testing.
        module('mean');

        inject(function($rootScope,$controller){
            // create a scope object for us to use.
            $scope = $rootScope.$new();

            SharedController = $controller('shared.StatsController',{
                $scope:$scope
            });
        });
    });
    it('Should contain a user object',function(){
        // User cannot be undefined
        expect($scope.test).toEqual('yoo');
    });

});

angular 控制器看起来像这样:

// Create the 'shared' controller
angular.module('shared').controller('shared.StatsController', [ '$scope',
    function($scope) {
        $scope.test = 'yoo';
    }
]);

Angular 版本是 1.4 并且 karma 依赖是版本:

"karma": "~0.12.23",
"karma-jasmine": "~0.2.2",
"karma-phantomjs-launcher": "~0.1.4",

我一整天都在为这件事伤脑筋。我希望对测试 angular 有更多了解的人可以帮助我。

您的 angular 控制器未正确实例化。要 'get' 或加载模块,您可以使用 angular.module('moduleName', []),第二个参数是具有依赖关系的数组。所以,应该是:

angular.module('shared', []).controller('StatsController', [ '$scope',
    function($scope) {
        $scope.test = 'yoo';
    }
]);