Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/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/Redux中清除所有订阅_Angular_Unit Testing_Ngrx_Subscription - Fatal编程技术网

单元测试是否已在Angular/Redux中清除所有订阅

单元测试是否已在Angular/Redux中清除所有订阅,angular,unit-testing,ngrx,subscription,Angular,Unit Testing,Ngrx,Subscription,我有一个使用ngrx的Angular应用程序,我使用TakeTill方法处理一个主题,在组件被销毁时清除订阅 private unsubscribe$ = new Subject(); this.store.select(actions.getTest) .pipe( takeUntil(this.unsubscribe$) ) .subscribe((numberOfSelectedRoles: number) => { }); ng

我有一个使用ngrx的Angular应用程序,我使用TakeTill方法处理一个主题,在组件被销毁时清除订阅

private unsubscribe$ = new Subject();

this.store.select(actions.getTest)
    .pipe(
        takeUntil(this.unsubscribe$)
    )
    .subscribe((numberOfSelectedRoles: number) => {

    });

ngOnDestroy() {
    this.unsubscribe$.next(true);
}

我决定使用这种方法,而不是保留订阅数组,因为它看起来更干净,但是有没有办法用Jasmine编写单元测试来检查组件是否没有打开的订阅

我们使用takeWhile和一个标志来代替。这可能更容易测试,因为您只需检查标志的值

import { Component, OnInit, OnDestroy } from '@angular/core';

import { Product } from '../product';

import { Observable } from 'rxjs';
import { takeWhile } from 'rxjs/operators';

/* NgRx */
import { Store, select } from '@ngrx/store';

@Component({
  selector: 'pm-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit, OnDestroy {
  componentActive = true;

  // Used to highlight the selected product in the list
  selectedProduct: Product | null;

  constructor(private store: Store<fromProduct.State>) { }

  ngOnInit(): void {

    // Subscribe here because it does not use an async pipe
    this.store.pipe(
      select(fromProduct.getCurrentProduct),
      takeWhile(() => this.componentActive)
    ).subscribe(
      currentProduct => this.selectedProduct = currentProduct
    );

  }

  ngOnDestroy(): void {
    this.componentActive = false;
  }

}