Blog

Blog

PHODAL

PhantomJS Jasmine GeoLocation 测试

在写Geolocation测试的时候,发现PhantomJS里面是没有geolocation的。

PhantomJS Geolocation 简单支持

由于只需要验证navigator.geolocation. getCurrentPosition被调用,可以简单地mock一下。声明一下这些函数

 window.navigator = function () {};
window.navigator.geolocation = function () {};
window.navigator.geolocation.getCurrentPosition = function () {};
window.navigator.userAgent = function () {};
window.navigator.userAgent.match = function () {};
window.navigator.userAgent.indexOf = function () {};

接着便可以写测试。

spyOn(window.navigator.geolocation, 'getCurrentPosition').andReturn();
geo.requestGeoLocation();
expect(window.navigator.geolocation.getCurrentPosition).toHaveBeenCalled();

PhantomJS Geolocation Mock

也在StackoverFlow上看到一个更详细的,只是不适合当前的情况,记录一下

// Replace real geolocation with mock geolocation
delete navigator.geolocation;    
navigator.geolocation = {
  isMock: true,
  paused: true,
  delay: 100,
  shouldFail: false,
  failsAt: -1,
  unFailsAt: -1,
  errorMessage: "There was an error retrieving the position!",
  currentTimeout: -1,
  lastPosReturned: 0,
  overrideAccuracy: 0, // accuracy in m to return for all positions (overrides any existing accuracies defined in the waypoints)
  useOverrideAccuracy: false, // Whether to override acurracies defined in the waypoints with the above override accuracy      
  _geoCall: function(method, success, error, repeat) {
      return this.currentTimeout = window[method].call(null, __bind(function() {
        var nextPos;
        if(!this.paused && this.lastPosReturned < this.waypoints.length - 1){
            nextPos = this.lastPosReturned++;               
        }else{
            this.lastPosReturned = nextPos = 0;
        }
        if(!this.shouldFail && nextPos == this.failsAt) this.shouldFail = true;
        if(this.shouldFail && nextPos == this.unFailsAt) this.shouldFail = false;
        if(repeat) this._geoCall("setTimeout", success, error, true);
        if (this.shouldFail && (error != null)) {               
            return error(this.errorMessage);
        }
        else if (success != null && !this.paused) {                
            var result = this.waypoints[nextPos];
            result.isMock = true;
            if(this.useOverrideAccuracy) result.coords.accuracy = this.overrideAccuracy;               
            success(result);
            return nextPos;
        }
      }, this), this.delay);

  },
  getCurrentPosition: function(success, error) {
    return this._geoCall("setTimeout", success, error, false);
  },
  watchPosition: function(success, error) {
    this._geoCall("setTimeout", success, error, true);
    return this.currentTimeout;
  },
  clearWatch: function(id) {
    return clearInterval(this.currentTimeout);
  },
  waypoints: []
};

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签