用茉莉花打字稿测试window.location.reload

时间:2018-08-22 08:07:54

标签: typescript karma-jasmine

在Jasmine中模拟window.location.reload()功能时遇到问题。

我有这个功能:

 public refresh(): void {
    if (this.selectedLanguage && this.selectedLanguage !== LanguageManager.selectedLanguage) {
      LanguageManager.changeSelectedLanguage(this.selectedLanguage);
      window.location.reload(true);
    }
  }

我尝试创建以下测试:

 it('should change language and reload page', () => {

    LanguageManager.selectedLanguage = LanguageManager.LANGUAGE_EN_EN;
    component.selectedLanguage = LanguageManager.LANGUAGE_RO_RO;
    spyOn(window.location, "reload").arguments(true).and.callFake(function () {
      console.log("fake reload");
    });
    component.refresh();
  });

我想模拟位置重载功能,但出现此错误:

Error: <spyOn> : reload is not declared writable or has no setter
Usage: spyOn(<object>, <methodName>)

我也尝试过:spyOn(window.location, "reload");,但是遇到了同样的错误。 有人能帮我吗? 谢谢。

0 个答案:

没有答案