如何伪造jasmine中geolocator的返回调用 [英] How to fake return calls from the geolocator in jasmine

查看:104
本文介绍了如何伪造jasmine中geolocator的返回调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用geolocator的函数,我不知道如何测试这个函数。我尝试过监视geolocator并返回假数据但没有成功,原始功能仍在使用,所以我将不得不等待,并且不能使用模拟数据。

  //这不起作用
var navigator_spy = spyOn(navigator.geolocation,'getCurrentPosition').andReturn({
coords:{
纬度:63,
经度:143
}
});

我该怎么做?

解决方案

当您调用地理位置代码时,它看起来像这样:

  navigator.geolocation。 getCurrentPosition(onSuccess,onError); 

这意味着您正在调用它并传递函数:

 函数onSuccess(position){
//用返回的坐标做一些事情
var myLat = position.coords.latitude;
var myLon = position.coords.longitude;


函数onError(错误){
//发生错误时执行某些操作
}

所以,如果你想用jasmine返回一个值来窥探它,你需要使用原始调用的第一个参数调用成功函数,如下所示: / p>

  spyOn(navigator.geolocation,getCurrentPosition)andCallFake(function(){
var position = {coords: {latitude:32,longitude:-96}};
arguments [0](position);
});

如果您想让它看起来像返回了一个错误,您会想要调用错误函数使用原始调用的第二个参数,如下所示:

  spyOn(navigator.geolocation,getCurrentPosition)。andCallFake(function (){
arguments [1](error);
});

编辑可显示完整示例:

这是你使用Jasmine测试的函数:

 函数GetZipcodeFromGeolocation(onSuccess,onError){ 
navigator.geolocation.getCurrentPosition(function(position){
//用位置信息做一些事情,比如调用
//一个带有ajax调用的web服务来获取数据
var (codecode)= CallWebServiceWithPosition(position);
onSuccess(zipcode);
},function(error){
onError(error);
});
}

这将在您的spec文件中:

  describe(Get Zipcode From Geolocation,function(){
it(应该用有效数据执行onSuccess函数,function(){
var jasmineSuccess = jasmine.createSpy();
var jasmineError = jasmine.createSpy();
$ b spyOn(navigator.geolocation,getCurrentPosition).CallFake(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');
});
});
});

此时,当您运行该规范时,它会告诉您,您的Web服务为您提供了如果您的网络服务正常运行,则为您提供的纬度和经度提供适当的邮政编码。


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:

  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.

这篇关于如何伪造jasmine中geolocator的返回调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆