Angularjs 无法访问jasmine测试中的数据对象

Angularjs 无法访问jasmine测试中的数据对象,angularjs,jasmine,leaflet,Angularjs,Jasmine,Leaflet,我们使用带有传单的angularjs来显示地图。此外,我们还使用jasmine(1.3.1.5)+maven进行测试。在编写的规范中,我们无法访问数据对象。控制器如下所示: angular.module('TestProject').controller('TestCtrl', ['$scope', 'leafletData', function TestCtrl($scope, leafletData){ 'use strict';

我们使用带有传单的angularjs来显示地图。此外,我们还使用jasmine(1.3.1.5)+maven进行测试。在编写的规范中,我们无法访问数据对象。控制器如下所示:

  angular.module('TestProject').controller('TestCtrl', ['$scope', 'leafletData',       
         function TestCtrl($scope, leafletData){
         'use strict';
          $scope.map;
          leafletData.getMap().then(function(map) {
                var southWest = L.latLng(-90, -180),
                northEast = L.latLng(90, 180),
                bounds = L.latLngBounds(southWest, northEast);
                mapObj.setMaxBounds(bounds);
                mapObj.options.maxZoom = 19;
                mapObj.options.minZoom = 1;
                $scope.map=map;
                $scope.featureGroup = L.featureGroup().addTo($scope.map);
      });
  }]);
控制器的规格为:

   describe("test controller",function() {
        var scope,leafletData, compile;

        beforeEach(module("TestProject"));

        beforeEach(inject(function($controller, $rootScope, $compile, leafletData) {
            scope = $rootScope.$new();
            compile = $compile;

            $controller('TestCtrl', {
                '$scope' : scope,
                'leafletData' : leafletData
            });
        }));

        it("test function", function() {
            var element = angular.element('<leaflet></leaflet>');
            element = compile(element)(scope);
            expect(leafletData).toBeDefined();
            leafletData.getMap().then(function(map) {
                scope.map = map;
            });
            $rootScope.$digest();
            expect(scope.map.getZoom()).toEqual(1);
            expect(scope.map.getCenter().lat).toEqual(0);
            expect(scope.map.getCenter().lng).toEqual(0);
        });

  });
描述(“测试控制器”,函数(){
变量范围,数据,编译;
之前(模块(“测试项目”);
beforeach(注入(函数($controller、$rootScope、$compile、数据){
scope=$rootScope.$new();
compile=$compile;
$controller('TestCtrl'{
“$scope”:范围,
“传单数据”:传单数据
});
}));
它(“测试函数”,函数(){
变量元素=角度元素(“”);
元素=编译(元素)(范围);
expect(传单数据).toBeDefined();
传单数据.getMap().then(函数(map){
scope.map=map;
});
$rootScope.$digest();
expect(scope.map.getZoom()).toEqual(1);
expect(scope.map.getCenter().lat).toEqual(0);
expect(scope.map.getCenter().lng).toEqual(0);
});
});
我们得到的错误是:


1.)测试控制器it测试功能未定义数据,因为它在测试顶部声明,但从未为其分配值。这是将注射剂引入测试时应遵循的一般模式:

describe("test controller",function() {
    var leafletData;

    beforeEach(module("TestProject"));

    beforeEach(inject(function(_leafletData_) {
        // without this, leafletData will be undefined
        leafletData = _leafletData_;
    }));

    it("should be defined", function() {
        expect(leafletData).toBeDefined();
    });
});
此外,在进行任何单元测试时,应单独测试被测代码。它所依赖的任何服务(即本例中的
数据
)以及来自这些服务的响应都应该被模拟。例如:

describe("test controller",function() {
    var $q;
    var scope;
    var leafletData;

    var mockMapData = {
        // ...
        // some mock map data
        // ...
    };

    beforeEach(module("TestProject"));

    beforeEach(inject(function(_$controller_, _$rootScope_, _$q_, _leafletData_) {
        scope = _$rootScope_.$new();
        $q = _$q_;
        leafletData = _leafletData_;

        // mock the leafletData
        spyOn(leafletData, 'getMap').andReturn($q.when(mockMapData));

        _$controller_('TestCtrl', {
            '$scope' : scope
        });
    }));

    it("test function", function() {
        scope.$digest();
        expect(scope.map).toEqual(mockMapData);
    });
});;

更重要的是,传递给控制器的注入数据隐藏了声明的var,当在测试中访问它时,该var没有定义。