javascript - 如何模拟文件,用茉莉花隐藏?
问题描述
我正在尝试document.hidden
在角度单元测试中进行模拟,但它不起作用。
我已经尝试过这些选项:
spyOn(Document.prototype, <any>'hidden').and.returnValue(true);
spyOn(Document, <any>'hidden').and.returnValue(true);
spyOn(document, <any>'hidden').and.returnValue(true);
spyOn(document, <any>'hidden').and.callFake(() => true);
spyOn(DOCUMENT, <any>'hidden').and.returnValue(true); // using TestBed
提前致谢
解决方案
间谍是为函数而设计的,而不仅仅是属性值。模拟一个属性就行
document.hidden = true;
更新:因为 hidden 是一个只读属性,我建议将文档对象注入组件,然后在 test 中为它提供您想要的任何值
class MyComponent {
constructor(@Inject(DOCUMENT) private document: Document) {}
...
}
// test
let documentMock: any;
...
documentMock = {hidden: true};
TestBed.configureTestingModule({
providers: [{provide: DOCUMENT, useValue: documentMock}]
})
推荐阅读
- highcharts - 绘制图表时如何隐藏线条
- android - Android POS 通过 NFC 和芯片接受 Google Pay
- vue.js - 如何通过点击事件访问或获取图表上特定图形的值?
- reactjs - 构建时如何防止 build/index.html 被删除
- c# - 从第三个表中检索数据
- android - 更改 SHA1 证书
- android - android studio中的BuildApk
- python - 用于树莓派开发的简单 python GUI
- grails - 哪个版本的 codenarc 将支持 Grails 1.3.7?
- javascript - Angular js中的自定义过滤器