2
我們正在使用帶有小葉的angularjs來顯示地圖。除此之外,我們使用茉莉花(1.3.1.5)+ maven進行測試。在寫入的規格中,我們無法訪問leafletData對象。控制器是這樣的:無法訪問茉莉花測試中的leafletData對象
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);
});
});
而且我們得到的錯誤是:
1)測試控制器也測試功能< < <失敗! *預計未定義將被定義。 (第22行)
我們已經在angular-leaflet-directive中特別導入了所有的js文件,但是在undefined中,它似乎沒有工作。我們也有這種感覺,這可能是角度模塊名稱差異的問題,我們嘗試使用beforeEach(module(「TestProject」,「leaflet-directive」));但這也沒有解決我們的問題。
請幫助我們!
甚至更多的是,傳遞給控制器的注入leafletData會隱藏聲明的var,在測試中訪問該var時會留下未定義的var。 – rayners 2014-12-12 16:05:00