【问题标题】:how to add navigator.geolocation.getCurrentPosition in phantomjs+jasmine+requirejs env如何在 phantomjs+jasmine+requirejs env 中添加 navigator.geolocation.getCurrentPosition
【发布时间】:2016-07-20 07:18:28
【问题描述】:

我只是 phantomjs + jasmine + requirejs + saga 来测试 JavaScript 代码。

我用这种方式来模拟navigator.geolocation.getcurrentposition跟随0 in Does phantomJS support geolocations?

我的 JavaScript 代码如下所示:

define(function (require) {

'use strict';
var authenticator = {};
var Authenticator = require("clientservice/authenticator/Authenticator");
var webpage = require('webpage');
describe("Request geolocation information", function(){
    beforeEach(function(){
        webpage.onInitialized = function() {
            webpage.injectJs('geolocation.js');
        };
        authenticator =  new Authenticator(configObj);
    });

    it("will request browser API for getting geolocation information", function(){
         authenticator.authenticate(mockController.successHandler);

     });
});

然后在运行 mvn clean package verify 时出现以下错误。

  • 已创建新会话:6246caf0-4e47-11e6-bc1f-bf8e9b2d1d0e [错误 - 2016-07-20T06:58:38.443Z] 会话 [6246caf0-4e47-11e6-bc1f-bf8e9b2d1d0e ] - page.onError - msg: Error: Script error for: 网页

有什么想法吗?

【问题讨论】:

  • authenticator.authenticate 调用 navigator.geolocation.getcurrentposition

标签: requirejs jasmine phantomjs navigator saga


【解决方案1】:

我尝试了另一种方法来模拟 navigator.geolocation:

if(navigator.geolocation === void 0) {
   console.log("It's Ok, we can mock one!");
}

navigator.geolocation = function() {};
navigator.geolocation.getCurrentPosition = function(callBack) {
    console.log("Mock navigator is invoked!");
    callBack();};

function callBack() {
   console.log("callBack is invoked!");
};

if (navigator.geolocation.getCurrentPosition === void 0) {
    console.log("Oh my god!");
}

navigator.geolocation.getCurrentPosition(callBack);

phantom.exit();

虽然调用了callBack函数,但是运行js的时候报错: 回调被调用! TypeError: 'undefined' 不是函数(评估 'navigator.geolocation.getC urrentPosition(callBack)()')

【讨论】:

    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    相关资源
    最近更新 更多