2015-09-16 87 views
0

有下面的代碼:茉莉花間諜函數調用

$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

是的。我理解你的想法,但你的代碼不起作用。你可以檢查它並編輯? – malcoauri

+0

對不起,我是茉莉花的新人 – malcoauri

回答

0

如果我理解正確的話,你想調用地理編碼功能的模擬,只是爲了檢查,如果你的控制器功能的處理是正確的,是嗎?

你應該用茉莉花間諜的「調用和返回值」,這可以讓您對函數的調用在測試和硬編碼您希望這個函數返回的結果。例如:

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