如何在茉莉花中伪造地理定位器的回电

我有一个调用地理定位器的函数,但我不知道如何测试这个函数.我已经尝试监视地理定位器并返回假数据,但没有成功,原始功能仍在使用,所以我不得不等待,我无法使用模拟数据.

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

我该怎么做?

推荐答案

调用地理定位代码时是这样的:

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
  }

因此,如果您想使用 jasmine 返回值来监视它,您需要使用原始调用的第一个参数调用成功函数,如下所示:

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

编辑以显示完整示例:

这是您使用 Jasmine 测试的功能:

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.

相关文章