有以下代码:
$scope.clickByPoint = function(marker, eventName, point) {
var geocoder, location;
$scope.options.info.point = point;
$scope.options.info.show = true;
$scope.searched = false;
$scope.address = "";
geocoder = new google.maps.Geocoder();
location = {
lat: parseFloat(point.latitude),
lng: parseFloat(point.longitude)
};
geocoder.geocode({ location: location }, function(results, status) {
$scope.searched = true;
if (status === google.maps.GeocoderStatus.OK) {
$scope.address = results[0].formatted_address;
}
return $scope.$digest();
});
};
请告诉我,我怎么可以'间谍'打电话给'geocoder.geocode'并执行假代码而不是它?提前致谢!
答案 0 :(得分:0)
如果我理解正确,你想要调用地理编码功能的模拟,只是为了检查你的控制器功能是否正确对待它,是吗?
您应该使用Jasmine Spy""调用并返回值",这样您就可以调用测试中的函数并硬编码您希望此函数返回的结果。例如:
describe("A spy, when configured to fake a return value", function() {
var foo, bar, fetchedBar;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
};
spyOn(foo, "getBar").and.returnValue(745);
foo.setBar(123);
fetchedBar = foo.getBar();
});
it("tracks that the spy was called", function() {
expect(foo.getBar).toHaveBeenCalled();
});
it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(745);
});
});
您可以在此处查看茉莉花文档以获取更多信息:http://jasmine.github.io/2.0/introduction.html#section-Spies:_and.returnValue