热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Irunmyangulartestswithkarma,myapplicationisrunningfineinbrowser,buttestsfailsandI

I run my angular tests with karma, my application is running fine in browser, but tests fails and I am suspecting wrong settings.

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

Here are the controllers and tests :

这是控制器和测试:

// app/scripts/controllers/main.js

'use strict';

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

  }); 

Here is the test file :

这是测试文件:

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

The karma conf

因果报应

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

The error ouput

错误输出

 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)

I am wondering why this happen, I tried to find some documentation about the karma initialization with angularjs. But the most documentation I found is only dummy tutorial that are repeating the same pattern ( like the dummy todo list, but with phones ... )

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

It seem that $controllerProvide.register fails to resolve my controllers name. But Directives tests are working correctly ...

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

Thanks for your attention.

感谢您的关注。

Edit Notes : I replaced the controller PersonCtrl by MainCtrl in this thread because It was confusing people about where to look. Now MainCtrl is the simpliest failing example I found.

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

This issue is only affecting my controllers, ( all of them ), but tests for Services and Directives are working as expected

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

4 个解决方案

#1


10  

I solved my problem, I've spent nearly a week to figure why this was not working.

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

I'd like to warn you, that Karma Stacktrace and error reports, even in debug mode, were not showing clues and were mainly missleading. I've spent time in Javascript debugger jumping frame to frame, to understand why my controllers where not loaded. ( Inspecting Angular's controllers register, shown it was empty)

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

While digging in my directories I've found a *.js that were not loaded in the index in production but by the globbing pattern in tests.

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

It was my old http_interceptor service that I moved but did not trashed the file. Removing this buggy file fixed the weird Karma/Jasmine/Angular behaviour.

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

Lesson learned : Do not trust tests output ( but what should I trust then ? ). Remove files you are not using/testing.

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

Thanks to everyone who tryied to solve this issue.

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

#2


8  

i think the main problem is coming from the 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'
    ],  

removing the * and specifying files one by one in the correct order, because if one is loaded before another it can break.

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

Edit : Add your files in the same order as your index.html

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

#3


1  

looking at this I'm wondering if the error message is confusing things.

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

in the stack trace i can see

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

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

and from the example it does seem as though you have not defined any properties on the scope in your controller.

从示例中看起来好像您没有在控制器中的范围上定义任何属性。

do you still have the problem if you add

如果添加,你还有问题吗?

$scope.awesomeThings = []; 

to your controller?

你的控制器?

#4


1  

If you're using the latest angular and also using the angular-route module, you should include the angular-route script in the karma conf file too:

如果您使用的是最新的角度并且还使用了角度路径模块,那么您也应该在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'
],  

I had this problem and adding it to the karma file did the trick.

我有这个问题,并将其添加到业力文件就行了。


推荐阅读
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • CentOS 6.5安装VMware Tools及共享文件夹显示问题解决方法
    本文介绍了在CentOS 6.5上安装VMware Tools及解决共享文件夹显示问题的方法。包括清空CD/DVD使用的ISO镜像文件、创建挂载目录、改变光驱设备的读写权限等步骤。最后给出了拷贝解压VMware Tools的操作。 ... [详细]
  • 本文介绍了如何清除Eclipse中SVN用户的设置。首先需要查看使用的SVN接口,然后根据接口类型找到相应的目录并删除相关文件。最后使用SVN更新或提交来应用更改。 ... [详细]
  • React项目中运用React技巧解决实际问题的总结
    本文总结了在React项目中如何运用React技巧解决一些实际问题,包括取消请求和页面卸载的关联,利用useEffect和AbortController等技术实现请求的取消。文章中的代码是简化后的例子,但思想是相通的。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文介绍了如何使用vue-awesome-swiper组件,包括在main.js中引入和使用swiper和swiperSlide组件,以及设置options和ref属性。同时还介绍了如何在模板中使用swiper和swiperSlide组件,并展示了如何通过循环渲染swipes数组中的数据,并使用picUrl属性显示图片。最后还介绍了如何添加分页器。 ... [详细]
  • uniapp开发H5解决跨域问题的两种代理方法
    本文介绍了uniapp开发H5解决跨域问题的两种代理方法,分别是在manifest.json文件和vue.config.js文件中设置代理。通过设置代理根域名和配置路径别名,可以实现H5页面的跨域访问。同时还介绍了如何开启内网穿透,让外网的人可以访问到本地调试的H5页面。 ... [详细]
author-avatar
让牙齿晒晒太阳诱惑
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有