問題描述
我有一個調(diào)用地理定位器的函數(shù),但我不知道如何測試這個函數(shù).我已經(jīng)嘗試監(jiān)視地理定位器并返回假數(shù)據(jù),但沒有成功,原始功能仍在使用,所以我不得不等待,我無法使用模擬數(shù)據(jù).
I have a function that calls the geolocator and i don't know how to test this function. I've tried spying on the geolocator and returning fake data but with no success, the original function is still used and so i would have to wait and i couldn't use mock data.
// this doesn't work
var navigator_spy = spyOn( navigator.geolocation, 'getCurrentPosition' ).andReturn( {
coords : {
latitude : 63,
longitude : 143
}
} );
我該怎么做?
推薦答案
調(diào)用地理定位代碼時是這樣的:
When you call the geolocation code, it looks like this:
navigator.geolocation.getCurrentPosition(onSuccess, onError);
這意味著您正在調(diào)用它并傳遞函數(shù):
This means that you're calling it and passing it functions:
function onSuccess(position) {
// do something with the coordinates returned
var myLat = position.coords.latitude;
var myLon = position.coords.longitude;
}
function onError(error) {
// do something when an error occurs
}
因此,如果您想使用 jasmine 返回值來監(jiān)視它,您需要使用原始調(diào)用的第一個參數(shù)調(diào)用成功函數(shù),如下所示:
So, if you wanted to spy on it using jasmine returning a value, you'd want call the success function using the first argument of the original call like this:
spyOn(navigator.geolocation,"getCurrentPosition").andCallFake(function() {
var position = { coords: { latitude: 32, longitude: -96 } };
arguments[0](position);
});
如果你想讓它看起來像返回了一個錯誤,你需要像這樣使用原始調(diào)用的第二個參數(shù)來調(diào)用錯誤函數(shù):
If you wanted to make it look like an error was returned, you'd want to call the error function using the second argument of the original call like this:
spyOn(navigator.geolocation,"getCurrentPosition").andCallFake(function() {
arguments[1](error);
});
編輯以顯示完整示例:
這是您使用 Jasmine 測試的功能:
This is the function you are using Jasmine to test:
function GetZipcodeFromGeolocation(onSuccess, onError) {
navigator.geolocation.getCurrentPosition(function(position) {
// do something with the position info like call
// an web service with an ajax call to get data
var zipcode = CallWebServiceWithPosition(position);
onSuccess(zipcode);
}, function(error) {
onError(error);
});
}
這將在您的規(guī)范文件中:
This would be in your spec file:
describe("Get Zipcode From Geolocation", function() {
it("should execute the onSuccess function with valid data", function() {
var jasmineSuccess = jasmine.createSpy();
var jasmineError = jasmine.createSpy();
spyOn(navigator.geolocation,"getCurrentPosition").andCallFake(function() {
var position = { coords: { latitude: 32.8569, longitude: -96.9628 } };
arguments[0](position);
});
GetZipcodeFromGeolocation(jasmineSuccess, jasmineError);
waitsFor(jasmineSuccess.callCount > 0);
runs(function() {
expect(jasmineSuccess).wasCalledWith('75038');
});
});
});
此時,當(dāng)您運行規(guī)范時,它會告訴您,如果您的網(wǎng)絡(luò)服務(wù)正常工作,您的網(wǎng)絡(luò)服務(wù)會為您提供與您提供的緯度和經(jīng)度對應(yīng)的正確郵政編碼.
At this point, when you run the spec, it will tell you that your web service gave you the proper zip code for the latitude and longitude you supplied if your web service works properly.
這篇關(guān)于如何在茉莉花中偽造地理定位器的回電的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!