[英]How to access window object from component in spec file angular 7
export class HomeComponent { constructor() { } gettableaudata(){ this.windowTableau.api= window; this.windowTab = this.windowTableau.api.tableau; return this.windowTab; } }
I have been trying to access window object of my component in my spec file, but the property( window.tableau) I am interested in is coming out to be undefined我一直在尝试在我的规范文件中访问我的组件的 window object,但是我感兴趣的属性(window.tableau)是未定义的
I tried storing the window.tableau in a local variable or a method inside a component and tried accessing in the spec file but it didn't work.我尝试将 window.tableau 存储在局部变量或组件内的方法中,并尝试在规范文件中访问,但它不起作用。
You can(or even, should) use a custom Provider for window and mock it:您可以(甚至应该)为 window 使用自定义 Provider 并模拟它:
In your TestBed:在您的测试床上:
const windowMock: Window = {
tableau: () => {doSomething()}
};
TestBed.configureTestingModule({
providers: [
...
{ provide: 'Window', useValue: windowMock }
]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.