0

Jasmine と angularJS アプリで BDD を試しています。私の要件は、ファクトリからデータを取得するビューに select 要素を作成することです。そのため、BDD の真の精神で、ビューを書き始める前に、最初にファクトリとコントローラーを書きます。

工場の私のテスト:

 describe('getTypeOfUnit', function(){
    it('should return typeofunits', inject(function(getTypeOfUnit){
        expect(getTypeOfUnit).not.toBeNull();
        expect(getTypeOfUnit instanceof Array).toBeTruthy();
        expect(getTypeOfUnit.length).toBeGreaterThan(0);
    })) ;
  });

したがって、データがnullではなく、配列であり、少なくとも1つのアイテムが含まれていることをテストしています。工場がないので失敗します。

これは、テストに合格するためのファクトリです。

angular.module('myApp.services', [])
  .factory('getTypeOfUnit', function(){
        var factory = ['Research Lab', 'Acedamic Unit', 'Misc'];
        return factory;
    });

今すぐコントローラーに。空のコントローラーは次のとおりです。

angular.module('myApp.controllers', [])
    .controller('describeUnitController',[function($scope){        
        console.log('exiting describeUnit');
    }]);

そしてコントローラーのテスト:

describe('controllers', function(){
    var describeScope;

    beforeEach(function(){
        module('myApp.controllers');
        inject(function($rootScope, $controller){
            console.log('injecting contoller and rootscope in beforeEach');
            describeScope  = $rootScope.$new();
            var describerController = $controller('describeUnitController', {$scope: describeScope});

        });
    }) ;

    it('should create non empty "typeOfUnitsModel"', function() {
        expect(describeScope["typeOfUnits"]).toBeDefined();
        var typeOfUnits =   describeScope.typeOfUnits;
        expect(typeOfUnits instanceof  Array).toBeTruthy();
        expect(typeOfUnits.length).toBeGreaterThan(0);
    });
});

そのため、コントローラーが空でない配列を返すことをテストしています。サービスと同じです。これらのテストは失敗します。次のステップは、コントローラーのスコープ オブジェクトにプロパティを定義することです。

.controller('describeUnitController',[function($scope){
        $scope.typeOfUnits = [];
        console.log('exiting describeUnit');
    }]);

TypeError: Cannot set property 'typeOfUnits' of undefined というエラーが表示されるようになりました

コントローラーがスコープを認識しないのはなぜですか? DI が自動的に利用可能にすると思っていましたか?

前もって感謝します。また、私のテストについてコメントしてください。

4

1 に答える 1

1

私のコードで2つの間違いが見つかりました:

  1. コントローラーは $scope について認識していません。理由がわからない。したがって、次のいずれかを実行できます。

    .controller('describeUnitController',['$scope',function($scope){ $scope.typeOfUnits = []; console.log('describeUnit を終了中'); }]);

また

.controller('describeUnitController',function describeUnitController($scope){
            $scope.typeOfUnits = [];
            console.log('exiting describeUnit');
        });

なぜこのようになっているのかわかりません。しかし、教訓は学んだ。

  1. 次に、次のようにサービスを使用しようとしました。

    .controller('describeUnitController',function describeUnitController($scope, GetTypeOfUnit){ $scope.typeOfUnits = getTypeOfUnit; });

これにより、有名な Error: unknown provider for GetTypeOfUnit が表示されました

どうやら、サービスを使用してテストに合格するには、サービス モジュールをファクトリ モジュールに追加する必要があります。しかし、私の app.js にはそれらがすべて定義されています。

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers','ui.bootstrap','$strap.directives'])

ただし、テスト中なので、コントローラー モジュールにもサービス モジュールをロードする必要があります。アプリが (ブラウザーのように) 読み込まれている場合は、これを行う必要はありません。

私はこれを正しく理解していますか?皆さん、ありがとうございました。

于 2013-04-24T02:24:10.397 に答える