小编Fra*_*lla的帖子

使用Sinon对window.location.href进行存根

我正在尝试测试一些客户端代码,为此我需要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字符串,也避免重定向(因为我在浏览器中测试)?

javascript mocha.js stubbing sinon

24
推荐指数
3
解决办法
2万
查看次数

标签 统计

javascript ×1

mocha.js ×1

sinon ×1

stubbing ×1