简体   繁体   English

arma茉莉角1.6.2测试筛选器引发错误未捕获的错误:[$ injector:unpr]

[英]Karma jasmine Angular 1.6.2 testing filter throws error Uncaught Error: [$injector:unpr]

I am working on testing my AngularJS code, and I am having a problem that all the answers I could find wouldn't fix. 我正在测试我的AngularJS代码,但遇到的问题是我无法找到所有答案。 I am trying to test a filter that looks like this: 我正在尝试测试一个看起来像这样的过滤器:

app.filter('capitalize', function(){
    return function(val){
        if( val && typeof val === 'string'){
            return val.charAt(0).toUpperCase() + val.substr(1).replace('_', ' ');
        }
    };
});

my app is defined as : 我的app定义为:

var app = angular.module('myApp', ["ui.router", "ui.bootstrap", "ngSanitize", "pascalprecht.translate", "angular-google-analytics", 
"ngStorage", "jsonFormatter", "selectize", "toaster"]);

My test looks like: 我的测试看起来像:

describe('Filters Unit Test Suite:', function() {
    beforeEach(function() {
        angular.mock.module('myApp');
    });

    describe('capitalize filter', function() {
        var capitalizeFilter;
        beforeEach(inject(function(_$filter_) {
            capitalizeFilter = _$filter('capitalize');
        }));

        it('should capitalize the first letter of a string', function() {
            expect(capitalizeFilter('hello')).toEqual('Hello');
        });
    });
});

Every piece of code here is pulled from a different file, namely filters.js , app.js , and fitler.test.js respectively. 每件这里代码从一个不同的文件,即拉filters.jsapp.jsfitler.test.js分别。 I used these documentations here and the official AngularJs testing page here . 我用这些单证这里和官方AngularJs测试页在这里

I already tested almost all the response similar to question like this asked here, but none seems to be working. 我已经测试了几乎所有与此处类似问题类似的响应,但是似乎没有一个起作用。

Your help will be greatly appreciated. 对你的帮助表示感谢。

I might have skipped a response, if you think so, please add the link in comment. 如果您认为这样,我可能已经跳过了回复,请在评论中添加链接。 I have spend almost three days looking, searching here on stackoverflow for the reason why, and to find how to correct that. 我花了将近三天的时间,在此处搜索stackoverflow的原因,并找到纠正方法。 That is too much of my time I already lost, and I feel like I am to close to this to see what I am missing, that's why I seek your help. 我已经失去了太多的时间,我觉得我要接近这个,看看我缺少什么,这就是为什么我寻求您的帮助。

Thank you. 谢谢。

Just in case, here is how I put the files in my karma config file: 以防万一,这是我将文件放入我的业力配置文件中的方式:

  files:[

  'src/main/resources/assets/bower_components/angular/angular.js',
   'src/main/resources/assets/bower_components/angular-ui-router/release/angular-ui-router.js',
   'src/main/resources/assets/bower_components/ngstorage/ngStorage.js',
   'src/main/resources/assets/bower_components/angular-mocks/angular-mocks.js',
  'src/main/resources/assets/bower_components/jquery/dist/jquery.min.js',
  'src/main/resources/assets/bower_components/bootstrap/dist/js/bootstrap.min.js',
  'src/main/resources/assets/js/vendor/ui-bootstrap-tpls-1.2.4.min.js',
  'src/main/resources/assets/bower_components/angular-sanitize/angular-sanitize.js',
  'src/main/resources/assets/bower_components/highcharts/highcharts.js',
  'src/main/resources/assets/bower_components/highcharts/highcharts-more.js',
  'src/main/resources/assets/bower_components/angular-translate/angular-translate.js',
  'src/main/resources/assets/bower_components/moment/min/moment.min.js',
  'src/main/resources/assets/bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.js',
  'src/main/resources/assets/bower_components/angular-google-analytics/dist/angular-google-analytics.min.js',
  'src/main/resources/assets/bower_components/json-formatter/dist/json-formatter.js',
  'src/main/resources/assets/bower_components/selectize/dist/js/standalone/selectize.js',
  'src/main/resources/assets/bower_components/angular-selectize2/dist/angular-selectize.js',
  'src/main/resources/assets/bower_components/AngularJS-Toaster/toaster.min.js',

  'src/main/resources/assets/js/angular/app.js',
  'src/main/resources/assets/js/angular/filters.js',
  'src/test/resources/assets/js/angular/filters.test.js'
]

错误讯息图片

The error I got on running your code is this: 我在运行代码时遇到的错误是这样的: 在此处输入图片说明 Here is the working fiddle. 是工作中的小提琴。

I believe you had not added the angular-ui library to it. 我相信您尚未向其中添加angular-ui库。 I added the library and it works fine. 我添加了库,它运行正常。 Add the following link and try your code again. 添加以下链接,然后重试您的代码。

http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.min.js

After a long run, I finally figured out why I was having the error I mentioned. 经过长时间的运行,我终于弄清楚了为什么遇到上面提到的错误。 With the help o your answer @clever_bassi, I went back, and found out the the AuthService was a module which is local into my project. 在您的答案@clever_bassi的帮助下,我回过头去,发现AuthService是我项目中本地的模块。 the service was defined by one of the developer I am working with, and I was looking for it in the wrong place. 该服务是由与我合作的一位开发人员定义的,而我在错误的位置寻找它。 And this showed me that I still have a long way to go learning the one page website which is AngularJS. 这告诉我,我仍然有很长的路要走学习AngularJS的一页网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM