Karma测试似乎没有加载我的Angularjs控制器

前端之家收集整理的这篇文章主要介绍了Karma测试似乎没有加载我的Angularjs控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用karma运行我的角度测试,我的应用程序在浏览器中正常运行,但测试失败,我怀疑设置错误.

这是控制器和测试:

  1. // app/scripts/controllers/main.js
  2.  
  3. 'use strict';
  4.  
  5. angular.module('GloubiBoulgaClientApp')
  6. .controller('MainCtrl',function ($scope) {
  7.  
  8. });

这是测试文件

  1. 'use strict';
  2.  
  3. describe('Controller: MainCtrl',function () {
  4.  
  5. // load the controller's module
  6. beforeEach(module('GloubiBoulgaClientApp'));
  7.  
  8. var MainCtrl,scope;
  9.  
  10. // Initialize the controller and a mock scope
  11. beforeEach(inject(function ($controller,$rootScope) {
  12. scope = $rootScope.$new();
  13. MainCtrl = $controller('MainCtrl',{
  14. $scope: scope
  15. });
  16. }));
  17.  
  18. it('should attach a list of awesomeThings to the scope',function () {
  19. expect(true).toBe(true);
  20. });
  21. });

因果报应

  1. module.exports = function(config) {
  2. config.set({
  3. // base path,that will be used to resolve files and exclude
  4. basePath: '',// testing framework to use (jasmine/mocha/qunit/...)
  5. frameworks: ['jasmine'],// list of files / patterns to load in the browser
  6. files: [
  7. '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'
  8. ],// list of files / patterns to exclude
  9. exclude: [],// web server port
  10. port: 8080,// level of logging
  11. // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
  12. logLevel: config.LOG_INFO,// enable / disable watching file and executing tests whenever any file changes
  13. autoWatch: true,// Start these browsers,currently available:
  14. // - Chrome
  15. // - ChromeCanary
  16. // - Firefox
  17. // - Opera
  18. // - Safari (only Mac)
  19. // - PhantomJS
  20. // - IE (only Windows)
  21. browsers: ['PhantomJS'],// Continuous Integration mode
  22. // if true,it capture browsers,run tests and exit
  23. singleRun: false
  24. });
  25. };

错误输出

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

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

好像$controllerProvide.register无法解析我的控制器名称.
但指令测试工作正常……

感谢您的关注.

编辑注释:我在这个帖子中用MainCtrl替换了控制器PersonCtrl,因为它让人们在哪里看起来很困惑.现在,MainCtrl是我发现的最简单的失败示例.

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

解决了我的问题,我花了将近一个星期来解释为什么这不起作用.

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

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

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

学过的知识 :
不要相信测试输出(但我应该相信什么?).
删除您未使用/测试的文件.

感谢所有试图解决这个问题的人.

猜你在找的Angularjs相关文章