Home > Blockchain >  Karma test failing : TypeError: this.cardManagementService.getLabelList(...).replace is not a functi
Karma test failing : TypeError: this.cardManagementService.getLabelList(...).replace is not a functi

Time:09-23

I am getting Karma error :

TypeError: this.cardManagementService.getLabelList(...).replace is not a function

Please check my code in TS and Spec file. I think I am making some mistake while writing usevalue for CardManagementService.

in TS file I have below code:

 setCardManagementLabels(): void {
        this.cardManagementLabels.cardNumber = this.cardManagementService.getLabelList('PROFILE.CARDMGMTTAB.CARDNUM');
    
 .---Some code here --- 
    this.cardManagementService.getLabelList('PROFILE.CARDMGMTTAB.SUSPENDMESSAGENOCOVERAGE')
            .replace('{suspended}', 'suspended').replace('{suspended}', 'suspended')....................}}

For spec file I have this :

providers: [ ........{provide: CardManagementService, useValue: {getLabelList: () => of([]), getEventV: () => of([])} },......]

    fit('should test setCardManagementLabels method for suspend and no coverage reason', () => {
          component.cardStatus = 'Suspended';
          component.debitCardSuspendClosedReason = 'NO_COVERAGE';
          component.setCardManagementLabels();
         });

CodePudding user response:

If you use useValue at the providers of a test you need to provide a mock instance, that has the methods from the covered code. As of([]) is returned by getLabelList() in the test, that does not have any method called replace.

I assume that this.cardManagementService.getLabelList() returns a string in the original code, so in your case useValue: {getLabelList: () => "a string mocked string" could by a good starting point.

  • Related