【发布时间】:2021-04-25 22:32:32
【问题描述】:
我正在尝试为组件中的变量(masters)编写测试用例,它是服务中的BehaviorSubject 变量。我无法找到编写测试用例的方法。以下是详细内容
// component code
export class MastersComponent implements OnInit, OnDestroy {
destroy$: Subject<boolean> = new Subject<boolean>();
masters: any[] = [];
constructor(private masterService: MasterService) { }
ngOnInit(){
//.... some code here
this.masterService.masters.pipe(
takeUntil(this.destroy$))
.subscribe(result => {
this.masters = result;
});
//....some code here
this.getMasters();
}
// component method code
getMasters() => {
const paginationObject = {
pageNo: this.pageIndex + 1,
sizePerPage: this.sizePerPage,
searchString: this.searchString,
order: 'asc',
orderBy: ['name'],
repositoryId: this.repositoryId
};
this.masterService.getMasters(paginationObject, alertError);
}
ngOnDestroy() {
this.masterService.masters.next([]);
this.destroy$.next(true);
this.destroy$.unsubscribe();
}
}
// service method code
masters: BehaviorSubject<Master[]> = new BehaviorSubject([]);
getMasters = (paginationObject, fail) => {
this.resources.getMasters(paginationObject,
(response) => {
this.masters.next(response);
}, (response) => {
this.masters.next([]);
fail(response);
});
}
【问题讨论】:
-
你到底想测试什么?
-
嗨,迈克,感谢您的回复。我正在尝试为在 MastersComponent 中声明的 masters 变量编写测试用例。但是这个变量是一个可观察的,它会在服务方法中得到更新。
标签: angular unit-testing components observable