久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在茉莉花中偽造地理定位器的回電

How to fake return calls from the geolocator in jasmine(如何在茉莉花中偽造地理定位器的回電)
本文介紹了如何在茉莉花中偽造地理定位器的回電的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我有一個調(diào)用地理定位器的函數(shù),但我不知道如何測試這個函數(shù).我已經(jīng)嘗試監(jiān)視地理定位器并返回假數(shù)據(jù),但沒有成功,原始功能仍在使用,所以我不得不等待,我無法使用模擬數(shù)據(jù).

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

我該怎么做?

推薦答案

調(diào)用地理定位代碼時是這樣的:

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

  navigator.geolocation.getCurrentPosition(onSuccess, onError);

這意味著您正在調(diào)用它并傳遞函數(shù):

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 返回值來監(jiān)視它,您需要使用原始調(diào)用的第一個參數(shù)調(diào)用成功函數(shù),如下所示:

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

如果你想讓它看起來像返回了一個錯誤,你需要像這樣使用原始調(diào)用的第二個參數(shù)來調(diào)用錯誤函數(shù):

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

這將在您的規(guī)范文件中:

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

此時,當(dāng)您運行規(guī)范時,它會告訴您,如果您的網(wǎng)絡(luò)服務(wù)正常工作,您的網(wǎng)絡(luò)服務(wù)會為您提供與您提供的緯度和經(jīng)度對應(yīng)的正確郵政編碼.

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.

這篇關(guān)于如何在茉莉花中偽造地理定位器的回電的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈?zhǔn)椒椒ㄟM(jìn)行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 欧美jizzhd精品欧美巨大免费 | 黄a免费看 | 日韩不卡在线 | 欧产日产国产精品国产 | 久久精品视频亚洲 | 久久av一区 | 欧洲成人免费视频 | 成人在线看片 | 久久精品亚洲 | 99精品电影 | 久久久91精品国产一区二区精品 | 欧美日韩精品影院 | 日韩电影一区二区三区 | 国产精品久久久久久久岛一牛影视 | 久久av在线播放 | 久久9视频| 日韩欧美在线一区 | 国产日韩久久 | 7777奇米影视 | 一级免费毛片 | 久久精品99国产精品日本 | 99久久久国产精品 | 亚洲 欧美 日韩 在线 | 日韩高清在线观看 | 91网站在线看 | 国户精品久久久久久久久久久不卡 | 亚洲社区在线 | 在线成人www免费观看视频 | 在线视频99 | 精品国产乱码久久久久久闺蜜 | 国产精品久久久久久久久久 | 日韩在线免费视频 | 免费国产一区二区 | 国产高清在线精品一区二区三区 | 天天干夜夜操 | www.蜜桃av | 欧美午夜精品 | 精品视频一区二区三区在线观看 | 国产精品久久久久久久久图文区 | 911网站大全在线观看 | 91福利网|