单元测试是否已在Angular/Redux中清除所有订阅
我有一个使用ngrx的Angular应用程序,我使用TakeTill方法处理一个主题,在组件被销毁时清除订阅单元测试是否已在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
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;
}
}