未定义不是构造函数AngularJS Jasmin / Karma错误

时间:2018-12-03 19:13:57

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

我正在尝试为AngularJS代码运行单元测试用例。但是,当我使用app.js中的应用名称时,会引发错误。在特定控制器中初始化模块时,它工作正常。

对于此示例,我的测试文件和src文件位于同一文件夹中。

Karma.conf.js

l

gruntfile.js

error[E0597]: `l` does not live long enough
 --> src/lib.rs:6:36
  |
6 |     let words = lines.flat_map(|l| l.split_whitespace());
  |                                    ^                  - `l` dropped here while still borrowed
  |                                    |
  |                                    borrowed value does not live long enough
7 | }
  | - borrowed value needs to live until here

sum.spec.js

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

    basePath: '../',    
    frameworks: ['jasmine'],
    files: [
        'assets/libs/angular/angular.min.js',
        'assets/libs/angular/angular-mocks.js',

        'app/**/*.js',
        'app/**/*.html',
        'tests/sum.js',
        'tests/sum.spec.js'
    ],
    exclude: [
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false,
    plugins: [
      'karma-phantomjs-launcher',
      'karma-jasmine',
      'karma-ng-html2js-preprocessor',
      'karma-coverage',
      'karma-junit-reporter',
      'karma-threshold-reporter',
      'karma-htmlfile-reporter'
    ],
    concurrency: Infinity
  })
}

sum.js

module.exports = function(grunt) {

    grunt.initConfig({

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


        });

      grunt.loadNpmTasks('grunt-karma');

  };

但是当我将控制器附加到如下所示的应用程序名称时,单元测试不会成功。

app.js

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

    beforeEach(angular.mock.module('cookbook'));
  var $controller;

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

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

sum.js(不起作用)

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

有什么方法可以使用应用程序名称初始化控制器,而不是在每个控制器中都使用angular.module吗?

0 个答案:

没有答案