vendredi 16 octobre 2020

Here is my component code and unit test case for it test case is success but the code inside subscribe is not getting coverage

my component.ts file

getAllType() {
      this.typeRefService.getAllTypes().subscribe((data) => {
        this.types = data.associationTypes;
        if(this.types.length > 0) {
          this.showNoData = false;
        }else {
          this.showNoData = true;
        }
        for (let i = 0; i < this.types.length; i++) {
          this.checkboxItems[i] = false;
          this.codes[i] = this.types[i].code;
        }
        // //console.log(this.types);
      });
    }

my component.spec.ts file

     it('should call a service', () => {
    const associationType= {
      "code": "ALSO_BOUGHT",
      "description": "Product's that are also bought with the given product",
      "name": "string",
      "createdBy": "createuser",
      "modifiedBy": "modifyuser"
    }

   spyOn(component , 'getAllType').and.callThrough()

    service = fixture.debugElement.injector.get(AssociationTypeRefServiceService);//get your service
    // spyOn(service,'getAllTypes').and.callFake(()=>{
    //   return of([associationType])

    // })

    spyOn(service,'getAllTypes').and.returnValue(of(associationType))
    service.getAllTypes()
    console.log(service.getAllTypes())
    expect(service.getAllTypes).toHaveBeenCalledWith()
    
    expect(component.getAllType).toBeTruthy()
    expect(associationType.code).toEqual('ALSO_BOUGHT')

    //
  });

While running unit test it passes but the code inside subscribe((data) => { does not get coverage . What am i doing wrong ? i am using jasmine karma and angular8

Aucun commentaire:

Enregistrer un commentaire