因为以下原因导致Karma / Jasmine测试失败:TypeError:MysmartdevicesCtrl未定义

时间:2016-10-17 14:50:00

标签: javascript angularjs jasmine karma-runner

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

错误日志:

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
    });
};

1 个答案:

答案 0 :(得分:0)

使用迷你匹配模式(/ ** / *):

'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'      
    ],