【问题标题】:Karma/Jasmine tests are failing due to: TypeError: MysmartdevicesCtrl is undefinedKarma/Jasmine 测试失败的原因是:TypeError: MysmartdevicesCtrl is undefined
【发布时间】:2017-02-26 14:56:25
【问题描述】:

由于控制器未定义,我的 angularjs 测试都失败了。我尝试了几种不同的解决方案,但没有任何效果。我已经添加了我认为在 karma 中需要的每个 js 文件,从外观上看,我不认为 beforeEach(inject... 函数被调用,或者如果它是那么某些东西没有得到正确设置。我添加了打印语句一路走来,但在 beforeEach(inject... 函数调用中没有打印任何内容。

错误日志:

Firefox 49.0.0 (Ubuntu 0.0.0) Controller: RegisteruserCtrl should attach a list of awesomeThings to the scope FAILED
    minErr/<@bower_components/angular/angular.js:68:12
    loadModules/<@bower_components/angular/angular.js:4640:15
    forEach@bower_components/angular/angular.js:321:11
    loadModules@bower_components/angular/angular.js:4601:5
    createInjector@bower_components/angular/angular.js:4523:19
    workFn@bower_components/angular-mocks/angular-mocks.js:3074:44
    [3]</ContextKarma/this.loaded@http://localhost:9876/context.js:151:7
    TypeError: RegisteruserCtrl is undefined in test/spec/controllers/registeruser.js (line 21)
    @test/spec/controllers/registeruser.js:21:5
    [3]</ContextKarma/this.loaded@http://localhost:9876/context.js:151:7
Firefox 49.0.0 (Ubuntu 0.0.0): Executed 3 of 3 (3 FAILED) ERROR (0.17 secs / 0.09 secs)

registerCtrl.js 规范:

'use strict';

describe('Controller: RegisteruserCtrl', function () {

  // load the controller's module
  beforeEach(module('clientApp'));

  var RegisteruserCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    RegisteruserCtrl = $controller('RegisteruserCtrl', {
      $scope: scope
      // place here mocked dependencies
    });
  }));

  it('should attach a list of awesomeThings to the scope', function () {
    expect(RegisteruserCtrl.awesomeThings.length).toBe(3);
  });
});

karma.conf.js

// Karma configuration
// Generated on Tue Oct 11 2016 18:25:32 GMT-0500 (CDT)
"use strict";
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'],


        // list of files / patterns to load in the browser
        files: [

            'bower_components/angular/angular.js',
            'bower_components/angular-route/angular-route.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'app/**/app.js',
            'app/Common/*.js',
            'app/scripts/controllers/*.js',
            'test/**/*.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: {},

        plugins: [
            'karma-firefox-launcher',
            'karma-chrome-launcher',
            'karma-jasmine',
        ],

        // 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: ['Firefox'],


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

【问题讨论】:

    标签: javascript angularjs jasmine karma-runner


    【解决方案1】:

    使用 minimatch 模式 (/**/*):

    'app/**/app.js',
    'app/Common/*.js',
    

    给我带来了很多麻烦。而不是这样做显式地写下你需要的控制器(或文件)的名称。例如:

    app/Common/RegisteruserCtrl.js
    

    这是一个适合我的例子:

     files: [
          './node_modules/angular/angular.js',                             // angular
          './node_modules/angular-mocks/angular-mocks.js',                 // angular-mocks
          './app/views/js/controllers/sugestionController.js'      
        ],
    

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多