2014-10-27 55 views
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”));但这也没有解决我们的问题。

请帮助我们!

回答

2

leafletData未定义,因为它是在测试的顶部声明的,但从未有赋值给它。这是把注射进你的测试时要遵循的一般模式:

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(); 
    }); 
}); 

而且,这样做的任何单元测试时,测试代码应该孤立测试。应该嘲笑它所依赖的任何服务(即在这种情况下为leafletData)以及来自这些服务的响应。例如:

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); 
    }); 
});; 
+1

甚至更​​多的是,传递给控制器​​的注入leafletData会隐藏声明的var,在测试中访问该var时会留下未定义的var。 – rayners 2014-12-12 16:05:00