【发布时间】:2016-08-09 06:22:28
【问题描述】:
我正在尝试测试一些客户端代码,为此我需要使用 Mocha/Sinon 存根 window.location.href 属性的值。
到目前为止我所尝试的 (using this example):
describe('Logger', () => {
it('should compose a Log', () => {
var stub = sinon.stub(window.location, 'href', 'http://www.foo.com');
});
});
跑步者显示如下错误:
TypeError:自定义存根应该是函数或属性描述符
将测试代码改为:
describe('Logger', () => {
it('should compose a Log', () => {
var stub = sinon.stub(window.location, 'href', {
value: 'foo'
});
});
});
产生此错误:
TypeError: 试图将字符串属性 href 包装为函数
将函数作为第三个参数传递给sinon.stub 也不起作用。
有没有办法提供一个假的window.location.href 字符串,同时避免重定向(因为我在浏览器中测试)?
【问题讨论】:
标签: javascript mocha.js sinon stubbing