Javascript Jasmine/PhantomJS忽略了定义者方法
我正在使用Jasmine 2.0进行一些测试,我有一个类似以下代码的测试:Javascript Jasmine/PhantomJS忽略了定义者方法,javascript,jasmine,phantomjs,Javascript,Jasmine,Phantomjs,我正在使用Jasmine 2.0进行一些测试,我有一个类似以下代码的测试: describe('My feature test', function () { describe('Negative asserts', function () { it('Expect location.search not to equal pre-defined value', function () { var myQueryValue = '?custom_q
describe('My feature test', function () {
describe('Negative asserts', function () {
it('Expect location.search not to equal pre-defined value', function () {
var myQueryValue = '?custom_query=true';
expect(myQueryValue).not.toEqual(window.location.search);
});
});
describe('Positive asserts', function () {
beforeEach(function () {
window.location.__defineGetter__('search', function () {
return '?custom_query=true';
});
});
it('Expect location.search to equal pre-defined value', function () {
var myQueryValue = '?custom_query=true';
console.log(window.location.search);
expect(myQueryValue).toEqual(window.location.search);
});
});
});
我正在使用grunt任务使用karma运行此代码,并启动Google Chrome(karma Chrome launcher
)和PhantomJS(karma PhantomJS launcher
)浏览器
在谷歌浏览器中,测试完全通过
LOG: '?custom_query=true'
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 99 of 99 SUCCESS (2.889 secs / 2.761 secs)
但是,当测试在PhantomJS中运行时,\uuu defineGetter\uuu
方法被忽略:
LOG: ''
PhantomJS 1.9.8 (Mac OS X) My feature test Positive asserts Expect location.search to equal pre-defined value FAILED
PhantomJS 1.9.8 (Mac OS X): Executed 99 of 99 (1 FAILED) (2.528 secs / 2.522 secs)
知道为什么会这样吗
编辑:
已尝试
对象。defineProperty
但仍不工作。PhantomJS中的defineProperty
方法失败,出现以下错误:
试图更改不可配置属性的访问机制
我找不到一个地方可以清楚地记录这一点,但是上面的错误让我怀疑这些属性是否在PhantomJS中是不可修改的
遇到相同的问题,在这种情况下,建议的解决方案是使用简单的用户定义函数包装不可重写的方法,这些函数可以在测试中重写:
// Wrap search property for testing purposes
function getLocationSearch() {
return window.location.search;
}
事实上,我已经这样做了,这似乎是处理来自PhantomJS的不可重写met/道具的最佳解决方案。非常感谢你的帮助。