我正在尝试测试一些客户端代码,为此我需要window.location.href
使用Mocha/Sinon 来存储属性的值.
到目前为止我尝试过的(使用此示例):
describe('Logger', () => {
it('should compose a Log', () => {
var stub = sinon.stub(window.location, 'href', 'http://www.foo.com');
});
});
Run Code Online (Sandbox Code Playgroud)
运行器显示以下错误:
TypeError:自定义存根应该是函数或属性描述符
将测试代码更改为:
describe('Logger', () => {
it('should compose a Log', () => {
var stub = sinon.stub(window.location, 'href', {
value: 'foo'
});
});
});
Run Code Online (Sandbox Code Playgroud)
这产生了这个错误:
TypeError:尝试将字符串属性href包装为函数
将函数作为第三个参数传递sinon.stub
也不起作用.
有没有办法提供假window.location.href
字符串,也避免重定向(因为我在浏览器中测试)?