最佳答案
目前,我正在为我的项目实现单元测试,有一个包含 window.location.href
的文件。
我想对此进行模拟测试,下面是我的示例代码:
it("method A should work correctly", () => {
const url = "http://dummy.com";
Object.defineProperty(window.location, "href", {
value: url,
writable: true
});
const data = {
id: "123",
name: null
};
window.location.href = url;
wrapper.vm.methodA(data);
expect(window.location.href).toEqual(url);
});
但我得到了这个错误:
TypeError: Cannot redefine property: href
at Function.defineProperty (<anonymous>)
我尝试了一些解决方案,但没有解决它。我需要一些提示来帮助我摆脱这个麻烦。请帮帮我。