Unable to access leafletData in specs.

164 views
Skip to first unread message

G Bhargava

unread,
Oct 27, 2014, 2:55:51 AM10/27/14
to jasmi...@googlegroups.com
We are working on angularjs and displaying map on page using leaflet. Along with this we are using jasmine(1.3.1.5) for the tests.

I have a test controller as:

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

And the spec for it is:

"use strict";

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

The error we get on running the spec:

  1.) test controller it test function <<< FAILURE!
    * Expected undefined to be defined.
    * TypeError: 'undefined' is not an object (evaluating 'leafletData.getMap') in http://localhost:49594/spec/controllers/org/testCtrlSpec.js (line 22)

We have specifically imported all js files available within leaflet-directive. But still leafletData is undefined. We had the feeling that this could be due to the difference in module name. Therefore I tried "beforeEach(module("TestProject", "leaflet-directive"));" but still it doesn't work. Seems we are missing out on some configuration. Please help!!

Andrés Muñoz

unread,
Nov 29, 2014, 10:02:10 PM11/29/14
to jasmi...@googlegroups.com
Hello,

Try with use an stub (A fake object can be detected but no pass to test) to leaflet in the unit test, because the leaflet object is reading in the test but for jasminejs es undefined,
I found a little example in this thread:

regards,

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.



--

Andrés Muñoz.

Reply all
Reply to author
Forward
0 new messages