运行Angular jasmine测试的例外与grunt业力

时间:2017-01-01 18:31:18

标签: angularjs jasmine karma-runner karma-jasmine

我已经安装并重新安装了grunt,karma和jasmine,但仍然存在以下问题。如果我在Jasmine独立运行测试,测试工作正常。似乎不是路径问题。如果我使用grunt karma命令运行它,我会收到以下错误:

这是我的Gruntfile.js:

module.exports = function(grunt) {

  grunt.initConfig({

        karma: {
          unit: {
            configFile: 'c:/tools/karma.conf.js',
            autoWatch: true
          }
        }


      });

    grunt.loadNpmTasks('grunt-karma');

};

这是我的karma.conf.js:

module.exports = function(config) {
  config.set({

    basePath: 'c:/tools/public/',    
    frameworks: ['jasmine'],
    files: [
        'libs/angular/angular.min.js',
        'libs/angular/angular-mocks.js',
        'test/cookbookspec.js'
    ],
    exclude: [
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: true,
    concurrency: Infinity
  })
}

这就是我得到的错误:

C:\tools\npm>grunt karma

Running "karma:unit" (karma) task
01 01 2017 13:17:52.333:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
01 01 2017 13:17:52.333:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
01 01 2017 13:17:52.364:INFO [launcher]: Starting browser PhantomJS
01 01 2017 13:17:53.883:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#9TpClXEcWPipDleTAAAA with id 35045843
PhantomJS 2.1.1 (Windows 8 0.0.0) cookbook level 1 cookbook level 2 should assign the correct rapper to scope FAILED
        Error: [$injector:modulerr] http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=cookbook&p1=%5B%24injector%3Anomod%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.8%2F%24injector%2Fnomod%3Fp0%3Dcookbook%0Ahttp%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A24%3A198%0Ab%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A23%3A252%0Ahttp%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A23%3A707%0Ahttp%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A38%3A119%0An%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A7%3A337%0Ag%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A37%3A562%0Aeb%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular.min.js%3F2215b8267ff3ed8ed0cee49c9f49f3925cc41d0c%3A41%3A250%0AworkFn%40http%3A%2F%2Flocalhost%3A9876%2Fbase%2Flibs%2Fangular%2Fangular-mocks.js%3F41fb2d656d9a871440ee139e6491d0e2b124ebc8%3A2428%3A60%0AattemptSync%40http%3A%2F%2Flocalhost%3A9876%2FC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1950%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1938%3A20%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A371%3A28%0Afn%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2579%3A44%0AattemptAsync%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1980%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1935%3A21%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Afn%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2564%3A31%0AattemptAsync%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1980%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1935%3A21%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Afn%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2564%3A31%0AattemptAsync%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1980%3A28%0Arun%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1935%3A21%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A1923%3A13%0AqueueRunnerFactory%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A714%3A42%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A2426%3A25%0Aexecute%40http%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fjasmine-core%2Flib%2Fjasmine-core%2Fjasmine.js%3F916005cc407925f4764668d61d04888d59258f5d%3A776%3A24%0Ahttp%3A%2F%2Flocalhost%3A9876%2FabsoluteC%3A%2Ftools%2Fnpm%2Fnode_modules%2Fkarma-jasmine%2Flib%2Fadapter.js%3F7a813cc290d592e664331c573a1a796192cdd1ad%3A336%3A23%0Aloaded%40http%3A%2F%2Flocalhost%3A9876%2Fcontext.js%3A151%3A17%0Aglobal%20code%40http%3A%2F%2Flocalhost%3A9876%2Fcontext.html%3A44%3A28 in libs/angular/angular.min.js (line 38)
        libs/angular/angular.min.js:38:429
        n@libs/angular/angular.min.js:7:337
        g@libs/angular/angular.min.js:37:562
        eb@libs/angular/angular.min.js:41:250
        workFn@libs/angular/angular-mocks.js:2428:60
        loaded@http://localhost:9876/context.js:151:17
        TypeError: undefined is not a constructor (evaluating '$controller('MainCtrl', {
                                        $scope: $scope
                                })') in test/cookbookspec.js (line 16)
        test/cookbookspec.js:16:32
        loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.016 secs / 0.009 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

更新

安装未缩小版的AngularJS之后,我收到以下消息:

C:\tools\npm>grunt karma
Running "karma:unit" (karma) task
01 01 2017 13:39:53.734:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
01 01 2017 13:39:53.734:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
01 01 2017 13:39:53.734:INFO [launcher]: Starting browser PhantomJS
01 01 2017 13:39:55.283:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#wB8sLyHoRK3D3WWhAAAA with id 75708888
PhantomJS 2.1.1 (Windows 8 0.0.0) cookbook level 1 cookbook level 2 should assign the correct rapper to scope FAILED
        forEach@test/angular.js:340:24
        loadModules@test/angular.js:4419:12
        createInjector@test/angular.js:4344:22
        workFn@libs/angular/angular-mocks.js:2428:60
        loaded@http://localhost:9876/context.js:151:17
        test/angular.js:4459:53
        TypeError: undefined is not a constructor (evaluating '$controller('MainCtrl', {
                                        $scope: $scope
                                })') in test/cookbookspec.js (line 16)
        test/cookbookspec.js:16:32
        loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0 secs / 0.009 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

我正在测试的控制器:

angular.module('cookbook', [])
.controller('MainCtrl', function($scope) {
    $scope.sum = function () {
        $scope.result = $scope.x + $scope.y;
    }
});

茉莉花测试:

describe('cookbook level 1', function () {

    beforeEach(angular.mock.module('cookbook'));

    var $controller;

    beforeEach(angular.mock.inject(function(_$controller_){
      $controller = _$controller_;
    }));

    describe('cookbook level 2', function () {
        it('should assign the correct rapper to scope', function () {
            var $scope = {};
            var controller = $controller('MainCtrl', { 
                $scope: $scope
            });
            $scope.x = 1;
            $scope.y = 2;
            $scope.sum();
            expect($scope.result).toBe(1+2);
        }); 
    });

1 个答案:

答案 0 :(得分:0)

在karma.conf.js中定义files所有依赖项(例如angular.js),所有测试(测试目录)和您正在测试的所有控制器(src目录):

files: [
        'libs/angular/angular.min.js',
        'libs/angular/angular-mocks.js',
        'src/cookbook.js'
        'test/cookbookSpec.js'
    ],