首页 > 解决方案 > Angular Jasmin 如何为以下基于组件订阅的组件代码编写单元测试代码

问题描述

我正在 Angular 中处理测试用例,但无法在以下代码库上取得进展。

构造函数(私有路由器:路由器,私有 cd:ChangeDetectorRef,私有 dbs:DashboardService,){ const navigation = this.router.getCurrentNavigation(); if (navigation) { const state = navigation.extras.state as any; if (state) { this.memberView = state; if (state.Ids.length) { this.Ids.setValue('Ids', state.userIds); } this.dbs.emitViewUpdated({ ind: 1, Ids: state.userIds }); } } }

      ngOnInit() {
        this.loadCards = false;
        this.Ids= [];
        this.subscription = this.dbs.membersUpdated$.subscribe((ids: any) => {
          this.cd.detectChanges();
          this.Ids= ids;
          this.loadCards = true;
        });
        this.Ids= this.dbs.getValue('Ids');
        this.loadCards = this.Ids?.length ? true : false;
      }
    
    Spec.ts File
it('ngoninit', fakeAsync(() => {
         const ids = [1, 2]
        dbs.membersUpdated$ = of(true);
        component.ngOnInit();
      }));


I am very in new Angular test case. Thanks in Advance

标签: angularunit-testingkarma-jasmineangular-unit-test

解决方案


推荐阅读