Karma错误:myApp未定义

时间:2021-05-13 18:55:21

I'm trying to test the Angular seed project with a clean install of Karma in a different directory. I haven't modified the Angular seed project. Why are myApp and myApp.View1Ctrl not defined?

我正在尝试在另一个目录中干净安装Karma来测试Angular种子项目。我没有修改Angular种子项目。为什么没有定义myApp和myApp.View1Ctrl?

My tests are:

我的测试是:

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

The output is:

输出是:

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)                                                      

My karma.conf has the following section, and I confirmed that the files exist:

我的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'
    ],                      

Please let me know if I need to post more details. I tried to read through all of the similar questions, and haven't found an answer.

如果我需要发布更多详细信息,请告诉我。我试图通读所有类似的问题,但没有找到答案。

1 个解决方案

#1


2  

You have myApp variable that is not defined in your spec. Define it:

您的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();
    });
});

There is also a related jsfiddle demonstrating the case.

还有一个相关的jsfiddle展示了这个案例。

#1


2  

You have myApp variable that is not defined in your spec. Define it:

您的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();
    });
});

There is also a related jsfiddle demonstrating the case.

还有一个相关的jsfiddle展示了这个案例。