How to fake return calls from the geolocator in jasmine

433 views
Skip to first unread message

Nicola Peluchetti

unread,
Jun 19, 2012, 1:16:06 PM6/19/12
to jasmi...@googlegroups.com

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
   
}
} );

anyone knows how i can do this?

Davis W. Frank

unread,
Jun 20, 2012, 1:37:19 AM6/20/12
to jasmi...@googlegroups.com
Can you post a more complete test example?

--dwf

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/DXD-iyACRyUJ.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.



--
thx,
--dwf

Nicola Peluchetti

unread,
Jun 20, 2012, 8:38:44 AM6/20/12
to jasmi...@googlegroups.com
Hi dave, my need is simply to fake the calls to the geolocator. I have a working test that runs like this

if( !! navigator.geolocation ) {
it( "The map should be initialized with data from the geolocator if it is present and no coordinates are set", function() {
// Wait for the loading of the gmaps. This is simply when the <div> we use as a container has some html
waitsFor( function() {
return typeof gmaps_helper.get_position() !== 'undefined';
}, "gmaps never loaded", 15000 );
runs( function() {
expect( gmaps_helper.get_marker().position.lat() ).toBeCloseTo( gmaps_helper.get_position().coords.latitude, 5 );
expect( gmaps_helper.get_marker().position.lng() ).toBeCloseTo( gmaps_helper.get_position().coords.longitude, 5 );
} );
} );
} else {
it( "The map should be initialized with standard data if the geolocator is not present and no coordinates are set", function() {
// Wait for the loading of the gmaps. This is simply when the <div> we use as a container has some html
waitsFor( function() {
return $('#ai1ec_map_canvas').html() !== '';
}, "gmaps never loaded", 15000 );
runs( function() {
expect( gmaps_helper.get_marker().position.lat() ).toEqual( 9.965 );
expect( gmaps_helper.get_marker().position.lng() ).toEqual( -83.327 );
} );
} );
}

This works, but i have to wait for the geolocator to answer and that is slow. I can't run that test every time. And the geolocator function seems not to be spiable upon

On Wednesday, 20 June 2012 07:37:19 UTC+2, Davis W. Frank wrote:
Can you post a more complete test example?

--dwf

On Tue, Jun 19, 2012 at 10:16 AM, Nicola Peluchetti <nicola.p...@gmail.com> wrote:

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
   
}
} );

anyone knows how i can do this?

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/DXD-iyACRyUJ.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.



--
thx,
--dwf

Reply all
Reply to author
Forward
0 new messages