Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单元测试:如何使用Angular+模拟窗口对象的innerWidth属性;茉莉花?_Angular_Unit Testing_Testing_Jasmine_Angular8 - Fatal编程技术网

单元测试:如何使用Angular+模拟窗口对象的innerWidth属性;茉莉花?

单元测试:如何使用Angular+模拟窗口对象的innerWidth属性;茉莉花?,angular,unit-testing,testing,jasmine,angular8,Angular,Unit Testing,Testing,Jasmine,Angular8,我想写一个单元测试,它期望一个变量包含一个特定的值,具体取决于窗口的innerWidth 在我的单元测试中,我使用window.innerWidth=1000。但是,我收到一个错误,它说无法分配给'innerWidth',因为它是只读属性。 代码(home.component.spec.ts): 有没有办法模拟innerWidth属性?不要认为这是可能的。作为一种解决方法,我创建了另一个变量来存储innerWidth的值,这样就可以正确地进行单元测试 public screenWidth: nu

我想写一个单元测试,它期望一个变量包含一个特定的值,具体取决于窗口的
innerWidth

在我的单元测试中,我使用
window.innerWidth=1000。但是,我收到一个错误,它说
无法分配给'innerWidth',因为它是只读属性。

代码(home.component.spec.ts):


有没有办法模拟
innerWidth
属性?

不要认为这是可能的。作为一种解决方法,我创建了另一个变量来存储
innerWidth
的值,这样就可以正确地进行单元测试

public screenWidth: number;

ngOnInit() {
   this.screenWidth = window.innerWidth; // Get initial innerWidth so the variable is immediately defined for reOrderCards() to recognise
   this.reOrderCards();
}


  @HostListener('window:resize', [])
  onResize() {
    this.screenWidth = window.innerWidth;
    this.reOrderCards();
  }

reOrderCards() {
if(this.screenWidth < 768) {
 // Do logic
}
公共屏幕宽度:数字;
恩戈尼尼特(){
this.screenWidth=window.innerWidth;//获取初始innerWidth,以便立即为reOrderCards()定义变量以识别
这个.reOrderCards();
}
@HostListener('窗口:调整大小',[])
onResize(){
this.screenWidth=window.innerWidth;
这个.reOrderCards();
}
reOrderCards(){
如果(此屏幕宽度<768){
//做逻辑
}

}不要认为这是可能的。作为一种解决方法,我创建了另一个变量来存储
innerWidth
的值,这样就可以正确地进行单元测试

public screenWidth: number;

ngOnInit() {
   this.screenWidth = window.innerWidth; // Get initial innerWidth so the variable is immediately defined for reOrderCards() to recognise
   this.reOrderCards();
}


  @HostListener('window:resize', [])
  onResize() {
    this.screenWidth = window.innerWidth;
    this.reOrderCards();
  }

reOrderCards() {
if(this.screenWidth < 768) {
 // Do logic
}
公共屏幕宽度:数字;
恩戈尼尼特(){
this.screenWidth=window.innerWidth;//获取初始innerWidth,以便立即为reOrderCards()定义变量以识别
这个.reOrderCards();
}
@HostListener('窗口:调整大小',[])
onResize(){
this.screenWidth=window.innerWidth;
这个.reOrderCards();
}
reOrderCards(){
如果(此屏幕宽度<768){
//做逻辑
}
}