gpt4 book ai didi

javascript - 使用 JavaScript 伪造 Google map 对象结构以进行单元测试

转载 作者:可可西里 更新时间:2023-11-01 01:24:22 26 4
gpt4 key购买 nike

Google Maps API 对象的使用方式如下:google.maps."Constructor" 看起来。

我想伪造下面列出的电话...

//var map = new google.maps.Map(value);
//var fireStationBound = new google.maps.LatLng(val,val);
//var bounds = new google.maps.LatLngBounds(boundary, boundary);
//var markerFireStation = new google.maps.Marker(val);
//var waterstationLayer = new google.maps.KmlLayer(val);
//map.mapTypes.set();

...使用下面的代码...

 $(document).ready(function() {

function google() {

/* todo: Setup for every constructor a test function returning the test data */

function LatLng(value) {

}

function LatLngBounds(value,value) {

}

function Marker(value) {

}

function Map(value) {

}

function KmlLayer(value) {

}

var maps = {
maps: {
LatLng: LatLng,
LatLngBounds: LatLngBounds,
Marker: Marker,
Map: Map,
KmlLayer: KmlLayer
}
};
return maps;
}

**// Works**
var google = new google();
var bound = new google.maps.LatLng(10);
var bounds = new google.maps.LatLngBounds(10,20);
var marker = new google.maps.Marker(10);
var layer = new google.maps.KmlLayer(10);
var map = new google.maps.Map(10);
**// Doesn't Work **
map.mapTypes.set();
});

我怎样才能伪造这个谷歌代码...

map.mapTypes.set();

...通过调用...返回

var map = google.maps.Map(val);

我的 JavaScript 必须是什么样子才能键入 map.mapTypes.set() 并最终调用 set 函数?

我只想伪造我在代码中用于单元测试的 Google 对象。

最佳答案

这是我们的 v3.19.18 版 Google map stub 。它最初是用 CoffeeScript 编写的,但我通过 js2coffee 运行了它这是一个原始的 Javascript 实现。我已经为您添加了 mapTypes.set()

window.stubGoogleAPIS = function () {
return window.google = {
maps: {
Animation: {},
BicyclingLayer: function() {},
Circle: function () {},
ControlPosition: {},
Data: function() {},
DirectionsRenderer: function() {},
DirectionsService: function() {},
DirectionsStatus: {},
DirectionsTravelMode: {},
DirectionsUnitSystem: {},
DistanceMatrixElementStatus: {},
DistanceMatrixService: function() {},
DistanceMatrixStatus: {},
ElevationService: function() {},
ElevationStatus: {},
FusionTablesLayer: function() {},
Geocoder: function() {},
GeocoderLocationType: {},
GeocoderStatus: {},
GroundOverlay: function() {},
ImageMapType: function () {},
InfoWindow: function() {},
KmlLayer: function() {},
KmlLayerStatus: {},
LatLng: function() {},
LatLngBounds: function() {},
MVCArray: function() {},
MVCObject: function() {},
Map: function () {
return {
setTilt: function () { },
mapTypes: {
set: function () { }
},
overlayMapTypes: {
insertAt: function () { },
removeAt: function () { }
}
};
},
MapTypeControlStyle: {},
MapTypeId: {
HYBRID: '',
ROADMAP: '',
SATELLITE: '',
TERRAIN: ''
},
MapTypeRegistry: function() {},
Marker: function() {},
MarkerImage: function() {},
MaxZoomService: function () {
return {
getMaxZoomAtLatLng: function () { }
};
},
MaxZoomStatus: {},
NavigationControlStyle: {},
OverlayView: function () { },
Point: function() {},
Polygon: function() {},
Polyline: function() {},
Rectangle: function() {},
SaveWidget: function() {},
ScaleControlStyle: {},
Size: function() {},
StreetViewCoverageLayer: function() {},
StreetViewPanorama: function() {},
StreetViewService: function() {},
StreetViewStatus: {},
StrokePosition: {},
StyledMapType: function() {},
SymbolPath: {},
TrafficLayer: function() {},
TransitLayer: function() {},
TransitMode: {},
TransitRoutePreference: {},
TravelMode: {},
UnitSystem: {},
ZoomControlStyle: {},
__gjsload__: function () { },
event: {
addListener: function () { }
},
places: {
AutocompleteService: function () {
return {
getPlacePredictions: function () { }
};
}
}
}
};

关于javascript - 使用 JavaScript 伪造 Google map 对象结构以进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16611747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com