我有以下代码,
ngOnInit() {
this.window.addEventListner('orienatationChange',() => {
hideSideNav();
this.publishOrientationChange(this.getOrientation(screen.orientation));
},false);
}
}
getOrientation(aScreenOrientation: ScreenOrienatation) : string {
if (aScreenOrienation.type.toString().toLowerCase().indexOf('landscape') > -1) {
return 'landscape';
} else {
return 'potrait'; --> Coverage is failing at else case
}
我的测试用例,
it ('should hide side navabr thrpough dispatch event', () => {
spyOn(component, 'getOrientation');
fixture.detectChanges();
window.dispatchEvent(new Evenet('orientationchange'));
expect(component.getOrientation).toHaveBeenCalled);
});
这里我正在尝试为该'getOrientation'函数的其他情况编写测试用例,我可以通过更改'screen.orientation'来做到这一点,但不幸的是这不是组件实例。我可以使用否则,可以。有人可以建议我帮忙。谢谢。