【问题标题】:Karma tests does not seem to load my Angularjs controller业力测试似乎没有加载我的 Angularjs 控制器
【发布时间】:2014-01-09 16:22:10
【问题描述】:

我使用 karma 运行我的角度测试,我的应用程序在浏览器中运行良好,但测试失败,我怀疑设置错误。

这里是控制器和测试:

// app/scripts/controllers/main.js

'use strict';

angular.module('GloubiBoulgaClientApp')
  .controller('MainCtrl', function ($scope) {

  }); 

这是测试文件:

'use strict';

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

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

  var MainCtrl,
    scope;  

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

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

因果报应

module.exports = function(config) {
  config.set({
    // base path, that will be used to resolve files and exclude
    basePath: '', 

    // testing framework to use (jasmine/mocha/qunit/...)
    frameworks: ['jasmine'],

    // list of files / patterns to load in the browser
    files: [
      'app/bower_components/angular/angular.js',
      'app/bower_components/angular-mocks/angular-mocks.js',
      'app/bower_components/angular-resource/angular-resource.js',
      'app/bower_components/angular-cookies/angular-cookies.js',
      'app/bower_components/angular-sanitize/angular-sanitize.js',
      'app/scripts/*.js',
      'app/scripts/**/*.js',
      'test/mock/**/*.js',
      'test/spec/**/*.js'
    ],  

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

    // web server port
    port: 8080,

    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel: config.LOG_INFO,
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    browsers: ['PhantomJS'],


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

错误输出

 PhantomJS 1.9.2 (Linux) Controller: MainCtrl should attach a list of awesomeThings to the scope FAILED
         Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined
         http://errors.angularjs.org/1.2.8-build.2094+sha.b6c42d5/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got% 2
 0undefined
             at assertArg (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angula
 r.js:1362)
             at assertArgFn (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angu
 lar.js:1373)
             at --obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angular.js:6763
             at --obfuscated-path--GloubiBoulga/GloubiBoulgaClient/test/spec/controllers/main.js:15
             at invoke (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular/angular.j
 s:3704)
             at workFn (--obfuscated-path--GloubiBoulga/GloubiBoulgaClient/app/bower_components/angular-mocks/angular -mocks.js:2120)

我想知道为什么会发生这种情况,我试图找到一些关于使用 angularjs 进行业力初始化的文档。但我发现的大多数文档只是重复相同模式的虚拟教程(如虚拟待办事项列表,但使用手机......)

$controllerProvide.register 似乎无法解析我的控制器名称。 但是指令测试工作正常......

感谢您的关注。

编辑注释:我在这个线程中用 MainCtrl 替换了控制器 PersonCtrl,因为它让人们不知道在哪里看。现在 MainCtrl 是我发现的最简单的失败示例。

此问题仅影响我的控制器(所有控制器),但服务和指令的测试按预期工作

【问题讨论】:

  • 尝试:将变量Person移出beforeEach(),因为模块Person应该首先定义
  • 刚刚编辑了代码。如您所见,问题仍然相同。
  • 我更改了示例。并使用了真正的虚拟代码来避免误导

标签: angularjs testing controller karma-runner


【解决方案1】:

我解决了我的问题,我花了将近一周的时间来弄清楚为什么这不起作用。

我想警告你,Karma Stacktrace 和错误报告,即使在调试模式下,也没有显示线索,主要是误导。 我花了一些时间在 javascript 调试器中跳帧,以了解为什么我的控制器没有加载。 (检查 Angular 的控制器寄存器,显示它是空的)

在我的目录中挖掘时,我发现了一个 *.js,它没有加载到生产中的索引中,而是通过测试中的通配模式加载。

我移动了旧的 http_interceptor 服务,但没有删除文件。 删除这个有问题的文件修复了奇怪的 Karma/Jasmine/Angular 行为。

经验教训: 不要相信测试输出(但我应该相信什么?)。 删除您不使用/测试的文件。

感谢所有尝试解决此问题的人。

【讨论】:

  • 谢谢!我一直在尝试调试为什么使用 Controller As 语法的测试不起作用。 (事实证明它们很好 ;-) 你的帖子提示我检查 karma.conf 正在加载什么,我注意到我需要添加 angular-route.js 和 app.routes.js。呸!
【解决方案2】:

我认为主要问题来自 karma conf:

files: [
      'app/bower_components/angular/angular.js',
      'app/bower_components/angular-mocks/angular-mocks.js',
      'app/bower_components/angular-resource/angular-resource.js',
      'app/bower_components/angular-cookies/angular-cookies.js',
      'app/bower_components/angular-sanitize/angular-sanitize.js',
      'app/scripts/*.js',
      'app/scripts/**/*.js',
      'app/scripts/**/**/*.js',
      'test/mock/**/*.js',
      'test/spec/**/*.js'
    ],  

删除 * 并以正确的顺序一一指定文件,因为如果一个在另一个之前加载,它可能会中断。

编辑:以与 index.html 相同的顺序添加文件

【讨论】:

  • 感谢您的回答,不幸的是它缺乏解释。并且不要解决问题。
  • 感谢您的尝试。但是我使用 phantom 和 chrome 在调试中运行 Karma 以检查发生了什么。文件按预期顺序加载。 (如上所列)。我只是在使用 $controller() 函数的地方设置了一个断点。但它没有找到 MainCtrl 我想知道如何按顺序列出可用的构造函数符号(如'MainCtrl')来检查我当前的上下文
  • 如果你在 app/scripts/controllers/main.js 文件的顶部放置一个调试器,你能看到业力停止吗?即你确定它真的在运行吗?
  • 不,它不评估这个文件。文件已加载但未评估。为什么我看不懂,还在找…… :(这真是令人沮丧
【解决方案3】:

看着这个,我想知道错误消息是否令人困惑。

在我可以看到的堆栈跟踪中

TypeError: 'undefined' is not an object (evaluating 'scope.awesomeThings.length')

从示例来看,您似乎没有在控制器的范围上定义任何属性。

如果你加了还有问题吗

$scope.awesomeThings = []; 

到你的控制器?

【讨论】:

  • 即使期望为真,它也会失败。问题主要是 $controllerProvider.register() 无法解析我的控制器名称。好像控制器没有加载。我会再次更新我的示例。
  • 我替换了期望部分以隔离奇怪的行为。
【解决方案4】:

如果您使用最新的angular 并同时使用angular-route module,您也应该在 karma conf 文件中包含 angular-route 脚本:

files: [
  'app/bower_components/angular/angular.js',
  'app/bower_components/angular-route/angular-route.js',
  'app/bower_components/angular-mocks/angular-mocks.js',
  'app/bower_components/angular-resource/angular-resource.js',
  'app/bower_components/angular-cookies/angular-cookies.js',
  'app/bower_components/angular-sanitize/angular-sanitize.js',
  'app/scripts/*.js',
  'app/scripts/**/*.js',
  'app/scripts/**/**/*.js',
  'test/mock/**/*.js',
  'test/spec/**/*.js'
],  

我遇到了这个问题,将它添加到 karma 文件就可以了。

【讨论】:

  • 感谢您的回答,不幸的是这不是问题,因为我不使用 ng-route 模块。我的控制器由测试浏览器加载,但从未得到评估。这是我的问题,它仍然存在;)
  • 我明白了。我有同样的问题,网络搜索把我带到了这个帖子。虽然这不是您的解决方案,但我不会删除我的答案,希望它能帮助其他遇到与我相同问题的线程的新手。祝您找到解决问题的方法!
  • “不要使用它”我的意思不是特别的,它只是包含在我的应用程序中,在某些服务中使用了 routeParams。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多