Testing branches jasmine angular

Deadly 提交于 2019-12-13 03:28:54

问题


I want to test following function:

     ngOnInit() {
        this.hmDataService.loadData().subscribe((data) => {
          this.description= data.description;
          this.date= data.date;
          const restoredID= this.localStorage.getItem('key');
          if (restoredID) {
            this.type = restoredID.toString();
            this.selectedMenu= this.option.filter((rf: any) => {
              return rf.value === restoredID;
            })[0];
          }
          this.createTable(this.id, this.data, this.type);
        });
  }

I need it for the test coverage which expect from me to test the branches.

    import { async, ComponentFixture, TestBed } from '@angular/core/testing';

    import { ChartComponent } from './chart.component';

    describe('ChartComponent', () => {
      let component: ChartComponent;
      let fixture: ComponentFixture<ChartComponent>;

      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ ChartComponent ]
        })
        .compileComponents();
      }));

      beforeEach(() => {
        fixture = TestBed.createComponent(ChartComponent);
        component = fixture.componentInstance;

      });

      it('should create', () => {
        expect(component).toBeTruthy();
      });
    it('should create table after page refresh', fakeAsync(() => {
      const restoredID = 'porsche';
      const spy = spyOn(component, 'createTableStructure');
      fixture.detectChanges();
      expect(component.selectedPaper.value).toEqual('porsche');
      expect(spy).toHaveBeenCalledTimes(1);
      }));
     });

How can I test it in right way? I need to test the branch and my restoreID variable is a function variable not global. The current test is not working

来源:https://stackoverflow.com/questions/50674096/testing-branches-jasmine-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!