Sunday, September 15, 2013

Geolocation unit test with JsTestDriver

Geolocation unit test with JsTestDriver

I'm testing geolocation using JsTestDriver, this is my code:
GeoLocationTest.prototype.testLocation = function(){
expectAsserts(1);
var coordinate = new Coordinate();
var location = coordinate.getLocation();
assertEquals("1,1",location);
};
Te test always fails because it tests immediately, before getting the
geolocation coordinates. I tried using a timeout but the test also
executes immediately.
setTimeout(function(){assertEquals("1,1",location);},10000);
And this is the javascript I'm trying to test
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: expected "1,1" but was "0,0"

No comments:

Post a Comment