我使用 Jasmine 和 PhantomJS 来运行测试用例。
在我的典型测试用例中,我调用服务电话,等待响应并确认响应。
有些请求可能会在几秒钟内返回,有些可能需要一分钟才能返回。
当通过 PhantomJS 运行时,测试用例因应该需要一分钟的服务调用而失败(失败是因为尚未收到响应)。
有趣的是,通过 Firefox 运行时测试通过了。
我尝试查看 tcpdump,并且通过两个浏览器的请求的 header 相同,因此这看起来像是浏览器超时问题。
有人遇到过类似的问题吗?关于在哪里可以配置超时有什么想法吗?或者您认为问题出在其他方面?
最佳答案
啊 PhantomJS 的痛苦。
显然我使用的是 javascript 的 bind
函数,PhantomJS 不支持该函数。
这导致测试失败,从而导致某些全局变量的状态困惑(我的错),从而导致失败。
但根本原因是使用bind
。
解决方案:尝试从 https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind 获取一个用于 bind
的垫片
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5 internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {},
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
关于timeout - PhantomJs超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16846506/