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
        getService@/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
        getService@/path/to/my-app/public/third-party/angular/angular.js:2862
        invoke@/path/to/my-app/public/third-party/angular/angular.js:2880
        workFn@/path/to/my-app/test/lib/angular/angular-mocks.js:1778

        angular.mock.inject@/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

どうもありがとう、

アルバン

4

2 に答える 2

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)」に置き換えます。これは、Angular が従うランダムな規則です。詳細については、このページのコメントを確認してくださいhttp://docs.angularjs.org/tutorial/ step_09

于 2013-06-21T07:03:16.957 に答える
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]);
}));

上記の回答で述べたように、これは魔法の言葉であるため、フィルター名に接尾辞「Filter」を付けることはできません。

シナリオを示すために、Plunkerも作成しました

于 2014-05-21T19:40:24.840 に答える