Karma AngularJS测试:没有发现测试,describe()似乎不存在

时间:2014-06-23 14:20:38

标签: angularjs intellij-idea jasmine karma-runner karma-jasmine

My Angular应用程序具有完美的单元测试功能。其他人重新组织了一些东西,为这个和其他相关项目添加了依赖关系的bower。从那以后,单元测试一直在失败。我现在正试图再次修复它。

当我开始时,我有一堆失败的单元测试。我运行bower来安装依赖项,但现在根本没有运行单元测试。当我查看单元测试的源代码时,我的IDE(IntelliJ)无法识别describe()beforeEach()it()函数。它确实识别module()inject()(均来自角度模拟)。

这是我的karma.conf.js:

module.exports = function (config) {
    config.set({
            basePath: '../',
            frameworks: ['jasmine'],
            files: [
                'app/lib/jquery/jquery.min.js',
                'app/lib/angular/angular.js',
                'app/lib/angular-*/angular-*.js',
                'app/lib/mylib-core/build/js/global.js',
                'app/lib/mylib-components/build/js/components.js',
                'app/js/*.js',
                'app/js/**/*.js',
                'test/unit/**/*Spec.js',
                'test/mocks/mockdata.js'
            ],
            plugins: [
                'karma-jasmine',
                'karma-junit-reporter',
                'karma-coverage',
                'karma-chrome-launcher',
                'karma-phantomjs-launcher'
            ],
            exclude: [
            ],
            reporters: ['progress', 'coverage'],
            preprocessors: {
                'app/js/**/*.js': 'coverage'
            },
            coverageReporter: {
                type: 'html',
                dir: 'coverage'
            },
            colors: true,
            logLevel: config.DEBUG,
            autoWatch: true,
            browsers: ['PhantomJS'],
            captureTimeout: 60000,
            singleRun: false
        }
    );
};

我单位的输出测试:

C:\Workset\tools\nodejs\node.exe C:\Workset\tools\npm\node_modules\karma\bin\karma start config/karma.conf.js
DEBUG [plugin]: Loading plugin karma-jasmine.
DEBUG [plugin]: Loading plugin karma-junit-reporter.
DEBUG [plugin]: Loading plugin karma-coverage.
DEBUG [plugin]: Loading plugin karma-chrome-launcher.
DEBUG [plugin]: Loading plugin karma-phantomjs-launcher.
DEBUG [plugin]: Loading inlined plugin (defining ).
INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
DEBUG [launcher]: Creating temp dir at C:\foo\Temp\karma-38315490
DEBUG [launcher]: C:\Workset\tools\npm\node_modules\karma-phantomjs-launcher\node_modules\phantomjs\lib\phantom\phantomjs.exe C:\foo\Temp\karma-38315490/capture.js
DEBUG [preprocessor.coverage]: Processing "C:/workspace/myproject/app/js/myproject.js".
DEBUG [preprocessor.coverage]: Processing "C:/workspace/myproject/app/js/controllers/FooController.js".
DEBUG [preprocessor.coverage]: Processing "C:/workspace/myproject/app/js/directives/fooRating.js".
DEBUG [preprocessor.coverage]: Processing "C:/workspace/myproject/app/js/myproject.js".
DEBUG [preprocessor.coverage]: Processing "C:/workspace/myproject/app/js/services/fooService.js".
DEBUG [watcher]: Resolved files:
C:/Workset/tools/npm/node_modules/karma-jasmine/lib/jasmine.js
C:/Workset/tools/npm/node_modules/karma-jasmine/lib/adapter.js
C:/workspace/myproject/app/lib/jquery/jquery.min.js
C:/workspace/myproject/app/lib/angular/angular.js
C:/workspace/myproject/app/lib/angular-animate/angular-animate.js
C:/workspace/myproject/app/lib/angular-animate/angular-animate.min.js
C:/workspace/myproject/app/lib/angular-cookies/angular-cookies.js
C:/workspace/myproject/app/lib/angular-cookies/angular-cookies.min.js
C:/workspace/myproject/app/lib/angular-loader/angular-loader.js
C:/workspace/myproject/app/lib/angular-loader/angular-loader.min.js
C:/workspace/myproject/app/lib/angular-mocks/angular-mocks.js
C:/workspace/myproject/app/lib/angular-resource/angular-resource.js
C:/workspace/myproject/app/lib/angular-resource/angular-resource.min.js
C:/workspace/myproject/app/lib/angular-route/angular-route.js
C:/workspace/myproject/app/lib/angular-route/angular-route.min.js
C:/workspace/myproject/app/lib/angular-sanitize/angular-sanitize.js
C:/workspace/myproject/app/lib/angular-sanitize/angular-sanitize.min.js
C:/workspace/myproject/app/lib/angular-scenario/angular-scenario.js
C:/workspace/myproject/app/lib/angular-touch/angular-touch.js
C:/workspace/myproject/app/lib/angular-touch/angular-touch.min.js
C:/workspace/myproject/app/js/myproject.js
C:/workspace/myproject/app/js/controllers/FooController.js
C:/workspace/myproject/app/js/directives/fooRating.js
C:/workspace/myproject/app/js/services/fooService.js
C:/workspace/myproject/test/unit/controllers/fooControllerSpec.js
C:/workspace/myproject/test/unit/directives/fooRatingSpec.js
C:/workspace/myproject/test/unit/services/fooServiceSpec.js
C:/workspace/myproject/test/mocks/mockdata.js
DEBUG [watcher]: Watching "C:/workspace/myproject/app/lib/jquery/jquery.min.js"
DEBUG [watcher]: Watching "C:/workspace/myproject/app/lib/angular/angular.js"
DEBUG [watcher]: Watching "C:/workspace/myproject/app/lib"
DEBUG [watcher]: Watching "C:/workspace/myproject/app/js"
DEBUG [watcher]: Watching "C:/workspace/myproject/test/unit"
DEBUG [watcher]: Watching "C:/workspace/myproject/test/mocks/mockdata.js"
DEBUG [web-server]: serving: C:\Workset\tools\npm\node_modules\karma\static/client.html
DEBUG [web-server]: serving: C:\Workset\tools\npm\node_modules\karma\static/karma.js
DEBUG [karma]: A browser has connected on socket 5Njwd5GANrKy9YV2vANC
INFO [PhantomJS 1.9.2 (Windows 7)]: Connected on socket 5Njwd5GANrKy9YV2vANC
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving: C:\Workset\tools\npm\node_modules\karma\static/context.html
DEBUG [web-server]: serving: C:/Workset/tools/npm/node_modules/karma-jasmine/lib/jasmine.js
DEBUG [web-server]: serving: C:/Workset/tools/npm/node_modules/karma-jasmine/lib/adapter.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/jquery/jquery.min.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular/angular.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-animate/angular-animate.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-animate/angular-animate.min.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-cookies/angular-cookies.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-cookies/angular-cookies.min.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-loader/angular-loader.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-loader/angular-loader.min.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-mocks/angular-mocks.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-resource/angular-resource.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-resource/angular-resource.min.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-route/angular-route.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-route/angular-route.min.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-sanitize/angular-sanitize.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-sanitize/angular-sanitize.min.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-scenario/angular-scenario.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-touch/angular-touch.js
DEBUG [web-server]: serving: C:/workspace/myproject/app/lib/angular-touch/angular-touch.min.js
DEBUG [web-server]: serving: C:\foo\Temp/b172f859347e22405c33fb8b757f7f6380c111f8.js
DEBUG [web-server]: serving: C:\foo\Temp/364a64513852785c67f39bc5bd6bacfd6d007792.js
DEBUG [web-server]: serving: C:\foo\Temp/221e578dd0aa30690853d25857681c2bdfe38fe6.js
DEBUG [web-server]: serving: C:\foo\Temp/75b9b08be8e14ecb83a385484eb40c5b551a3294.js
DEBUG [web-server]: serving: C:/workspace/myproject/test/unit/controllers/fooControllerSpec.js
DEBUG [web-server]: serving: C:/workspace/myproject/test/unit/directives/fooRatingSpec.js
DEBUG [web-server]: serving: C:/workspace/myproject/test/unit/services/fooServiceSpec.js
DEBUG [web-server]: serving: C:/workspace/myproject/test/mocks/mockdata.js
PhantomJS 1.9.2 (Windows 7): Executed 0 of 0 ERROR (0.363 secs / 0 secs)

我可以在那里看到我的* Spec.js文件,以及angular-mock.js和jasmine.js。并且它没有给出任何错误,但它认为有0个测试(我向你保证不是这样)。那么这里出了什么问题?

1 个答案:

答案 0 :(得分:2)

我明白了。调试输出提到它已加载C:/workspace/myproject/app/lib/angular-scenario/angular-scenario.js,因为我的karma.conf.js中有'app/lib/angular-*/angular-*.js'行。

angular-scenario.js的问题在于它是一个完全不同的jasmine测试框架,但它使用相同的语法,因此angular-scenario.js定义了jasmine.js定义的相同函数,覆盖了函数I需要与我不需要的。所以我的所有测试都使用angular-scenario注册,而不是使用jasmine。因此,当jasmine执行所有已注册的测试时,它找不到要执行的测试。

添加:

exclude: [
    'app/lib/angular-scenario/angular-scenario.js'
],

到我的karma.conf.js解决了这个问题。好吧,这个问题。我的测试还有很多其他问题,但我会先尝试自己解决这些问题。