[英]Jasmine spies function call
有以下代码:
$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”并执行伪代码而不是它? 提前致谢!
如果我理解正确,则您想对geocode函数进行调用,只是检查您的控制器函数是否正确处理了,对吗?
您应该使用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);
});
});
您可以在此处查看jasmine文档以获取更多信息: http : //jasmine.github.io/2.0/introduction.html#section-Spies : _and.returnValue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.