I have a service making reverse geocoding (translate coordinates into an address), and I use a promise to get the result.
When I try to test it, it does not resolve data even if I call the digest loop on the rootscope and it returns the test is passed even if conditions can't be satisfied.
Here it is the test file
'use strict';
describe('revGeocodeService', function() {
var revGeocodeService;
var resultAdd="";
beforeEach(function() {
module('angularApp');
});
var $rootScope;
beforeEach(inject(function(_revGeocodeService_,_$rootScope_) {
revGeocodeService=_revGeocodeService_;
$rootScope = _$rootScope_;
}));
it('should reverse position to an address', function() {
var position={};
position.latitude=51.5310315;
position.longitude=-0.06899029999999999;
revGeocodeService.getAddress(position).then(function(result)
{
resultAdd=result;
expect(1).toBe('somevalue');
});
$rootScope.$digest();
});
});
and here it is the service
'use strict';
(function(){
var revGeocodeService= function($q){
return{
getAddress : function(position){
console.log(position.latitude+" "+position.longitude);
//set up the promise
var deferred = $q.defer();
// set up the Geocoder object
var geocoder = new google.maps.Geocoder();
// turn coordinates into an object
var yourLocation = new google.maps.LatLng(position.latitude, position.longitude);
// find out info about our location
geocoder.geocode({ 'latLng': yourLocation }, function (results, status) {
if(status == google.maps.GeocoderStatus.OK) {
if(results[0]) {
console.log(results[0].formatted_address);
// return results[0].formatted_address;
console.log(results[0].formatted_address);
deferred.resolve(results[0].formatted_address);
} else {
error('Google did not return any results.');
deferred.resolve (null);
}
} else {
error("Reverse Geocoding failed due to: " + status);
deferred.resolve(null);
}
});
return deferred.promise;
}
}
}
angular.module('angularApp').factory('revGeocodeService',['$q',revGeocodeService]);
}());
Aucun commentaire:
Enregistrer un commentaire