使用 sinon 模拟 window.location.reload

发布于 2025-01-16 21:05:55 字数 247 浏览 0 评论 0原文

我正在使用 sinon 编写一段使用 window.location.reload(); 执行重新加载的 Vue 代码测试。

代码工作正常,但测试失败并出现错误 错误:未实现:导航(哈希更改除外)

如果我删除该行代码,测试不会崩溃。

我怎样才能编写一个通过该行代码正确运行的 sinon 测试? 我不确定是否需要保留该行,尽管我不确定如何为财产做到这一点。

有什么想法吗?

I'm writting tests with sinon for a section of Vue code that performs a reload with window.location.reload();.

The code works correctly, but the test is failing with an error Error: Not implemented: navigation (except hash changes)

If I delete that line of code, the tests don't crash.

How can I write a sinon test that runs correctly through that line of code?
I'm not sure if I need to stub that line, although I'm not sure how could I do it for a property.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2025-01-23 21:05:55

考虑 onbeforeunload ,它会引发错误并阻止实际的页面重新加载,而 assert.throws 会捕获错误。像这样的事情:

describe("#location.reload", () => {
  it("works well", () => {
    window.onbeforeunload = () => {
      throw new Error();
    };
    assert.throws(location.reload);
  });
});

Consider onbeforeunload which throws an error and prevents an actual page reloading, and assert.throws catching the error. Something like this:

describe("#location.reload", () => {
  it("works well", () => {
    window.onbeforeunload = () => {
      throw new Error();
    };
    assert.throws(location.reload);
  });
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文