Karma错误:myApp未定义

时间:2014-10-06 03:23:35

标签: angularjs unit-testing jasmine karma-runner karma-jasmine

我尝试在另一个目录中使用干净安装的Karma来测试Angular种子项目。我还没有修改过Angular种子项目。为什么myAppmyApp.View1Ctrl未定义?

我的测试是:

describe("Unit Testing Examples", function () {

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

    beforeEach(module('myApp'));

    it('should have a View1Ctrl controller', function () {
        expect(myApp).toBeDefined();
        expect(myApp.View1Ctrl).toBeDefined();
    });
});

输出结果为:

INFO [watcher]: Changed file "C:/Projects/KarmaJasmine/KarmaJasmine/test/placeho
lder.js".                                                                       
Chrome 37.0.2062 (Windows 8.1) Unit Testing Examples should have a View1Ctrl controller FAILED                                                                  
    ReferenceError: myApp is not defined                                    
        at null.<anonymous> (C:/Projects/KarmaJasmine/KarmaJasmine/test/plac
eholder.js:8:16)                                                                
Chrome 37.0.2062 (Windows 8.1): Executed 6 of 6 (1 FAILED) (0 secs / 0.054 secs)
Chrome 37.0.2062 (Windows 8.1): Executed 6 of 6 (1 FAILED) (0.061 secs / 0.054 s
ecs)                                                      

我的karma.conf有以下部分,我确认文件存在:

files: [
        '../AngularSeed/app/bower_components/angular/angular.js',
        '../AngularSeed/app/bower_components/angular-route/angular-route.js',
        '../AngularSeed/app/bower_components/angular-mocks/angular-mocks.js',
        '../AngularSeed/app/app.js',
        '../AngularSeed/app/components/**/*.js',
        '../AngularSeed/app/view*/**/*.js'
        , 'test/placeholder.js'
    ],                      

如果我需要发布更多详情,请告诉我。我试着阅读所有类似的问题,但没有找到答案。

1 个答案:

答案 0 :(得分:2)

您的规范中未定义myApp变量。定义它:

describe("Unit Testing Examples", function () {
    var myApp;

    beforeEach(function () {
        myApp = angular.module('myApp');
    });

    it('should have a View1Ctrl controller', function () {
        expect(myApp).toBeDefined();
        expect(myApp.controller('View1Ctrl')).toBeDefined();
    });
});

还有related jsfiddle证明了这一情况。