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 が自動的に利用可能にすると思っていましたか?
前もって感謝します。また、私のテストについてコメントしてください。