【问题标题】:Karma with AngularJS not working使用 AngularJS 的业力不工作
【发布时间】:2017-04-06 04:23:47
【问题描述】:

我正在使用 karma 和 jasmine 对 angular js 进行单元测试,但它会产生以下错误。

Chrome 54.0.2840 (Mac OS X 10.11.6) LOG: 'WARNING: Tried to load angular more than once.'

Chrome 54.0.2840 (Mac OS X 10.11.6) ERROR
  Uncaught TypeError: window.angular.$$csp is not a function
  at node_modules/angular/angular.js:31768

我已经附上了我的 karma.conf.js 文件和下面的 angular 模块。

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

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'ng-scenario'],


    // list of files / patterns to load in the browser
    files: [
        './node_modules/angular/angular.js',
        './node_modules/angular-ui-router/release/angular-ui-router.js',
        './node_modules/angular-mocks/angular-mocks.js',
        './app/**/*.js',
        './test/logger/loggerControllerTest.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

以下是角度模块创建sn-p。

var app = angular.module("gradchatApp", [
    ngRoute,
    ngAnimate,
    ngAria,
    ngMessages,
    ngResource,
    ngSanitize,
    ngTouch,
    ngCookies,
    angularMoment,
    angularMaterial,
    'ngDialog',
    'infinite-scroll',
    require( 'angular-bootstrap-npm' )
]);

请帮我解决上述错误。

【问题讨论】:

    标签: javascript angularjs node.js karma-jasmine


    【解决方案1】:

    您是否尝试使用单独的模板(使用 templateUrl)对指令进行单元测试?

    这发生在我之前在 templateUrl 中出错时,找不到模板,默认情况下,每个未找到的查询都被重定向到 index.html,而 index.html 又包括 angular.js。

    也许你也是同样的问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-10
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多