承诺不解析Jasmine测试中的数据 - Angular

时间:2015-02-14 21:23:34

标签: javascript angularjs unit-testing jasmine promise

我有一个服务进行反向地理编码(将坐标转换为地址),我使用一个承诺来获得结果。

当我尝试测试它时,即使我在根镜上调用摘要循环它也不会解析数据,即使条件不能满足,它也会返回测试。

这是测试文件

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

});

这里是服务

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

}());

0 个答案:

没有答案