2013-06-19 1 views
1

내 Angular.js 응용 프로그램에 대한 단위 테스트를 작성하려고하지만 필요한 항목을 주입 할 수 없습니다 (적합한 공급자를 찾을 수 없음).테스트를위한 누락 된 주입 (알 수없는 공급자)

나는 누구를보고 싶니?

Firefox 21.0 (Linux) filter staticList should convert static list object into its display value FAILED 
     Error: Unknown provider: staticListProvider <- staticList in /path/to/my-app/public/third-party/angular/angular.js (line 2734) 
     createInjector/providerInjector<@/path/to/my-app/public/third-party/angular/angular.js:2734 
     [email protected]/path/to/my-app/public/third-party/angular/angular.js:2862 
     createInjector/instanceCache.$injector<@/path/to/my-app/public/third-party/angular/angular.js:2739 
     [email protected]/path/to/my-app/public/third-party/angular/angular.js:2862 
     [email protected]/path/to/my-app/public/third-party/angular/angular.js:2880 
     [email protected]/path/to/my-app/test/lib/angular/angular-mocks.js:1778 

     [email protected]/path/to/my-app/test/lib/angular/angular-mocks.js:1764 
     @/path/to/my-app/test/unit/filtersSpec.js:19 
     @/path/to/my-app/test/unit/filtersSpec.js:16 
     @/path/to/my-app/test/unit/filtersSpec.js:3 

애플리케이션 :

angular.module('myApp', ['myAppFilters', 'ui.bootstrap', '$strap.directives']). 
// Some other stuff 

필터 :

"use strict"; 

angular.module('myAppFilters', []). 
    filter('staticList', function() { 
     return function (listItem) { 
      if (!listItem) { 
       return ''; 
      } 
      return listItem.value; 
     }; 
    });  

시험 :

'use strict'; 
describe('filter', function() { 

    beforeEach(angular.module('myAppFilters')); 

    describe('staticList', function() { 

     it('should convert static list object into its display value', 
      inject(function (staticList) { 
       expect(undefined).toBe(''); 
       expect({key: 'A', value: 'B'}).toBe('B'); 
      })); 
    }); 

});

카르마 구성 :

사람이 전체 코드를보고 싶어하면
basePath = '../'; 

files = [ 
    JASMINE, 
    JASMINE_ADAPTER, 
    'public/third-party/jquery/*.js', 
    'public/third-party/angular/angular.js', 
    'public/third-party/angular/i18n/angular-*.js', 
    'public/third-party/moment/moment.min.js', 
    'public/third-party/moment/moment-*.js', 
    'public/js/**/*.js', 
    'test/lib/**/*.js', 
    'test/unit/**/*.js' 
]; 

colors = true; 
autoWatch = true; 

browsers = ['Firefox']; 

junitReporter = { 
    outputFile: 'test_out/unit.xml', 
    suite: 'unit' 
}; 

, 응용 프로그램 저장소는 여기에 있습니다 : https://github.com/adericbourg/GestionCourrier

고마워,

알반

답변

3

에 코드 주입

it('should convert static list object into its display value', 
     inject(function (staticList) { 
      expect(undefined).toBe(''); 
      expect({key: 'A', value: 'B'}).toBe('B'); 
     })); 

"inject (function (staticList)")를 "inject (function (staticListFilter)"로 대체하십시오. 이것은 각도가 따라 오는 임의의 관례입니다. 이 페이지에서 의견을 확인하여 자세한 정보를 얻을 수 있습니다. http://docs.angularjs.org/tutorial/step_09

+0

나는 그 지점을 놓쳤다. 고마워요! –

+0

Thx! 이것은 문서에서 더 분명해야합니다. – Simon

1

비슷한 문제가 발생했는데, 제 경우에는 제 필터 이름에 '필터'라는 접미사가있어 동일한 분사 문제가 발생했습니다.

.filter('assetLabelFilter', function(){ 
    return function(assets, selectedLabels){ 
    // Implementation here 
    }; 
}); 

나는

'use strict'; 

    describe('assetLabelFilter', function() { 

    beforeEach(module('filters.labels')); 

    var asset1 = {labels: ['A']}; 
    var asset2 = {labels: ['B']}; 
    var asset3 = {labels: []}; 
    var asset4 = {labels: ['A', 'B']}; 
    var assets = [asset1, asset2, asset3, asset4]; 

    var assetLabelFilter; 

    beforeEach(inject(function($filter) { 
    assetLabelFilter = $filter('assetLabelFilter'); 
    })); 

    it('should return only assets with selected label', function() { 
    var selectedLabels = ['B']; 
    expect(assetLabelFilter(assets, selectedLabels)).toEqual([asset2, asset4]); 
    }); 
}); 

좋은 대답은 위에서 나를 깨닫게했다 마침내 수동으로 테스트에 필터를 주입하여 문제를 해결할 수 있었다 각 튜토리얼 방식으로 사용하려면 :

it('should ', inject(function(assetLabelFilter) { 
    var selectedLabels = ['B']; 
    expect(assetLabelFilter(assets, selectedLabels)).toEqual([asset2, asset4]); 
})); 

필터 이름은 위의 대답에서 언급 한 것처럼 마법의 단어이기 때문에 '필터'접미사를 사용할 수 없습니다.

내가 만든 시나리오를 설명하기 위해 Plunker

관련 문제