单元测试angularJS服务 [英] Unit testing a service in angularJS

查看:241
本文介绍了单元测试angularJS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的单元测试,但现在正任务是写一个现有的code基础,我一清二楚测试。

可悲的是,我无法做到什么那么也没有找到真正有用的文档的任何进展。

code基的主要组成部分是从API检索数据的服务,但我无法获得该模块的一个实例的服务属于:

 类型错误:模块不在/home/faebser/workspace/GridSense-CMS-App/dev/test/unit/api.test.js函数(13号线)

人缘配置: http://sprunge.us/ObSP?js

测试: http://sprunge.us/AJWL?js

人缘输出: http://sprunge.us/WYHI?bash

这是什么问题?为什么我不能够得到模块的实例?

UPDATE1:

我设法重新安装角度和角速度嘲笑相同版本来加载我的模块。但现在我遇到了以下错误:

<$p$p><$c$c>minErr/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:63:12
loadModules/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4138:15
forEach@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:323:11
loadModules@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4099:5
createInjector@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4025:11
workFn@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular-mocks/angular-mocks.js:2425:44

我设法跟踪误差降低到以下内容:

 [$喷油器:modulerr]未能实例化模块ui.router由于:[$喷油器:NOMOD]!模块'ui.router不可用你要么拼写错误的模块命名或忘记加载它。如果注册一个模块,确保您指定的依赖作为第二argument.http://errors.angularjs.org/1.3.15/$injector/nomod?p0=ui.routerminErr/<@http://localhost:9876/base/bower_components/angular/angular.js:63:12module/<@http://localhost:9876/base/bower_components/angular/angular.js:1774:1ensure@http://localhost:9876/base/bower_components/angular/angular.js:1698:38module@http://localhost:9876/base/bower_components/angular/angular.js:1772:1loadModules/<@http://localhost:9876/base/bower_components/angular/angular.js:4115:22forEach@http://localhost:9876/base/bower_components/angular/angular.js:323:11loadModules@http://localhost:9876/base/bower_components/angular/angular.js:4099:5loadModules/<@http://localhost:9876/base/bower_components/angular/angular.js:4116:40forEach@http://localho\"

好吧,只是忘了UI路由器添加到卡玛配置。


解决方案

好吧,我设法得到这个工作。
主要的问题是奇怪的错误信息的角度给我:

<$p$p><$c$c>minErr/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:63:12
loadModules/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4138:15
forEach@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:323:11
loadModules@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4099:5
createInjector@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4025:11
workFn@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular-mocks/angular-mocks.js:2425:44

要解决这个我用的业力调试窗口,并设置在angular.js断点:在功能minErr(模块,ErrorConstructor)63:

 返回新ErrorConstructor(消息);

这样,你可以看到的参数和编译错误消息。最后,我只是忘了在业力配置文件中添加一些依赖关系。

I'm relatively new to unit testing but was now tasked to write tests for a existing code base that I know quite well.

Sadly I'm unable to achieve any progress what so ever nor find really helpful documentation.

The main component of the code base is a service to retrieve data from an api but I'm unable to get an instance of the module the service belongs to:

TypeError: module is not a function in /home/faebser/workspace/GridSense-CMS-App/dev/test/unit/api.test.js (line 13)

karma config: http://sprunge.us/ObSP?js

tests: http://sprunge.us/AJWL?js

karma output: http://sprunge.us/WYHI?bash

What is the problem? why am I not able to get a instance of the module?

UPDATE1:

I'm managed to load my module by reinstalling the same version of angular and angular mocks. But now I run into the following error:

minErr/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:63:12
loadModules/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4138:15
forEach@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:323:11
loadModules@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4099:5
createInjector@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4025:11
workFn@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular-mocks/angular-mocks.js:2425:44

I managed to track the error down to the following:

"[$injector:modulerr] Failed to instantiate module ui.router due to:[$injector:nomod] Module 'ui.router' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.http://errors.angularjs.org/1.3.15/$injector/nomod?p0=ui.routerminErr/<@http://localhost:9876/base/bower_components/angular/angular.js:63:12module/<@http://localhost:9876/base/bower_components/angular/angular.js:1774:1ensure@http://localhost:9876/base/bower_components/angular/angular.js:1698:38module@http://localhost:9876/base/bower_components/angular/angular.js:1772:1loadModules/<@http://localhost:9876/base/bower_components/angular/angular.js:4115:22forEach@http://localhost:9876/base/bower_components/angular/angular.js:323:11loadModules@http://localhost:9876/base/bower_components/angular/angular.js:4099:5loadModules/<@http://localhost:9876/base/bower_components/angular/angular.js:4116:40forEach@http://localho"

Okay, simply forgot to add ui-router to karma-config.

解决方案

Okay, I managed to get this to work. The main problem was the strange error messages that angular gave me:

minErr/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:63:12
loadModules/<@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4138:15
forEach@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:323:11
loadModules@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4099:5
createInjector@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular/angular.js:4025:11
workFn@/home/faebser/workspace/GridSense-CMS-App/dev/bower_components/angular-mocks/angular-mocks.js:2425:44

To work around this I used the karma debug window and set a breakpoint at angular.js:63 in function minErr(module, ErrorConstructor):

return new ErrorConstructor(message);

that way you can see the arguments and the compiled error message. In the end i just forgot to add a few dependencies in the karma config file.

这篇关于单元测试angularJS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆