我正在使用 JsTestDriver 测试地理位置,这是我的代码:
GeoLocationTest.prototype.testLocation = function(){
expectAsserts(1);
var coordinate = new Coordinate();
var location = coordinate.getLocation();
assertEquals("1,1",location);
};
测试总是失败,因为它会在获取地理位置坐标之前立即进行测试。我尝试使用超时,但测试也立即执行。
setTimeout(function(){assertEquals("1,1",location);},10000);
这是我要测试的javascript
function Coordinate () {
this.latitude = 0.0;
this.longitude = 0.0;
this.date = new Date();
this.errorMsg = "";
}
Coordinate.prototype.getLocation = function(){
if (this.isBrowserSupported()){ //this test passes
navigator.geolocation.getCurrentPosition(this.setPosition,this.setError);
return "" + this.latitude + "," + this.longitude;
}
return "Browser not supported";
}
Coordinate.prototype.setPosition = function(position){
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
}
AssertError:预期为“1,1”但为“0,0”