AngularJS karma过滤器未知提供者

Dav*_*end 5 jasmine angularjs karma-runner angularjs-filter karma-jasmine

我做不到!我只是看了整个互联网,我尝试了其他解决方案,但我无法使其工作.

我有一个过滤器,我想用Karma和Jasmine进行测试.这是我的代码:

app.js:

var services = angular.module('myApp.services', [ 'ngResource' ]);

angular.module('myApp', [
    'ngRoute',
    'myApp.filters',
    'myApp.services',
    'myApp.directives',
    'myApp.controllers'
]).
Run Code Online (Sandbox Code Playgroud)

filters.js

'use strict';

/* Filters */

angular.module('myApp.filters', [])

.filter(
    'lowerAndCapital',
    [ function() {
        return function(text) {
            if (text != undefined) {
                var str = text.toLowerCase();
                var arreglo = str.split(" ");
                var result = "";
                for (var i = 0; i < arreglo.length; i++) {
                    result += " "
                            + arreglo[i].charAt(0).toUpperCase()
                            + arreglo[i]
                                    .substring(1, arreglo[i].length + 1);
                }
                return result;
            }
        }
    }]);
Run Code Online (Sandbox Code Playgroud)

filterSpecs.js

'use strict';

/* Jasmine specification for filters go here */

describe('filter', function() {

    beforeEach(function () {
        module('myApp.filters');
    });

    describe('lowerAndCapital', function() {
        it('deberia de convertir le texto a minuscula y con letra capital',
            inject(function(lowerAndCapital) {
                expect(lowerAndCapital("john")).toEqual(
                         "John");
        }));
    });
});
Run Code Online (Sandbox Code Playgroud)

karma.config.js

module.exports = function(config){
    config.set({

        basePath : '../',

        files : [
            'main/webapp/resources/scripts/angular/angular.js',
            'main/webapp/resources/scripts/angular/angular-route.js',
            'main/webapp/resources/scripts/angular/angular-mocks.js',
            'main/webapp/resources/js/*.js',
            'test/unit/*.js'
        ],

        autoWatch : true,

        frameworks: ['jasmine'],

        browsers : ['Chrome'],

        plugins : [
                'karma-chrome-launcher',
                'karma-firefox-launcher',
                'karma-jasmine',
                'karma-junit-reporter'
                ],

        junitReporter : {
          outputFile: 'test_out/unit.xml',
          suite: 'unit'
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

这是错误:

Chrome 36.0.1985 (Windows 7) filter lowerAndCapital deberia de convertir le text
o a minuscula y con letra capital FAILED
        Error: [$injector:unpr] Unknown provider: lowerAndCapitalProvider <- low
erAndCapital
        http://errors.angularjs.org/1.2.16/$injector/unpr?p0=lowerAndCapitalProv
ider%20%3C-%20lowerAndCapital
            at E:/workspace/GPS/src/main/webapp/resources/scripts/angular/angula
r.js:78:12
            at E:/workspace/GPS/src/main/webapp/resources/scripts/angular/angula
r.js:3705:19
            at Object.getService [as get] (E:/workspace/GPS/src/main/webapp/reso
urces/scripts/angular/angular.js:3832:39)
            at E:/workspace/GPS/src/main/webapp/resources/scripts/angular/angula
r.js:3710:45
            at getService (E:/workspace/GPS/src/main/webapp/resources/scripts/an
gular/angular.js:3832:39)
            at Object.invoke (E:/workspace/GPS/src/main/webapp/resources/scripts
/angular/angular.js:3859:13)
            at workFn (E:/workspace/GPS/src/main/webapp/resources/scripts/angula
r/angular-mocks.js:2147:20)
        Error: Declaration Location
            at window.inject.angular.mock.inject (E:/workspace/GPS/src/main/weba
pp/resources/scripts/angular/angular-mocks.js:2132:25)
            at null.<anonymous> (E:/workspace/GPS/src/test/unit/filtersSpec.js:1
5:5)
            at null.<anonymous> (E:/workspace/GPS/src/test/unit/filtersSpec.js:1
2:2)
            at E:/workspace/GPS/src/test/unit/filtersSpec.js:5:1
Chrome 36.0.1985 (Windows 7): Executed 1 of 1 (1 FAILED) ERROR (0.359 secs / 0.0
14 secs)
Run Code Online (Sandbox Code Playgroud)

PSL*_*PSL 12

您应该尝试注入filterprovider并获取lowerAndCapital过滤器.

var $filter;

beforeEach(function () {
   module('app');
});

beforeEach( inject(function (_$filter_) { //<-- Get the filter provider
   $filter = _$filter_;
}));

it('deberia de convertir le texto a minuscula y con letra capital',function(){
   expect($filter('lowerAndCapital')("john")).toEqual("John");
});
Run Code Online (Sandbox Code Playgroud)

Plnkr

或者使用"过滤器"将您的filtername Postfix并使用它

beforeEach( inject(function (_lowerAndCapitalFilter_) {
   $filter = _lowerAndCapitalFilter_;
}));
Run Code Online (Sandbox Code Playgroud)

过滤器只是将输入转换为输出的函数.但是,过滤器需要是Dependency Injected.为实现此目的,过滤器定义包含一个工厂函数,该函数使用依赖项进行注释并负责创建过滤器函数.

但是您的代码中存在一个错误,因为split(" ")您可能希望修剪返回的结果.

演示