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
}
} );
How can I do this?
When you call the geolocation code, it looks like this:
This means that you're calling it and passing it functions:
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:
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:
Edit to show full example:
This is the function you are using Jasmine to test:
This would be in your spec file:
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.
Ah wait, maybe you HAVE to create the spy within your
beforeEach
block because Jasmine restores spies automatically after each test case. if you did something like:the spy is already restored when you want to test it. Use this instead: