Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8493741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:06:18+00:00 2026-06-10T23:06:18+00:00

I have a function that calls the geolocator and i don’t know how to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:06:19+00:00Added an answer on June 10, 2026 at 11:06 pm

    When you call the geolocation code, it looks like this:

      navigator.geolocation.getCurrentPosition(onSuccess, onError);
    

    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
      }
    

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

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

    Edit to show full example:

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

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have link that calls a function when clicked: <a href=javascript:spawnMenu(this); id=link1>Test1</a> To make
I have a function that calls a test on each object. I want to
please help me understand this. You have a function that calls a few methods:
I have a javascript function that calls a web service. The data comeback (I
I have a function that calls out a read or write request on a
I have a function that calls itself to increment and decrement a stack. I
I have a javascript function that calls a generic function to make an ajax
I have a javascript that calls a function each 1 minute. That function sends
I have a function that makes a series of calls to sscanf() and then,
I have a function in my application that calls various subfunctions depending on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.