'Angular Jasmine testing - substring remove from number

I have some difficulties in unit testing a function. I'm new in these things and I don't know what I can do. I have to those 2 if.

Any advice? Thank you.



Solution 1:[1]

I think your issue is the jasmine.createSpy(phoneNumber);, since this method is under test, we should not spy on it but rather actually call its implementation.

Try the following:

it('should remove outside prefix', () => {
  const dialingOptions = {
    reqLongDistPrefix: 10, reqOutsidePrefix: 11, dialLongDistanceLength: 12
  } as any;
  coreSvc.getCCDef = jasmine.createSpy().and.returnValue({ DialingOptions: dialingOptions });
  let phoneNumber = '1011129999';
  // !! Remove the following line !!
  // component.removeOutsidePrefix = jasmine.createSpy(phoneNumber);
  const modifiedPhoneNumber = component.removeOutsidePrefix(phoneNumber);
  // !! below is not a good assertion, you can remove it
  // expect(Object.keys(dialingOptions).length).toBe(3);
  expect(modifiedPhoneNumber).toBe('insert what you expect it to be here');
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AliF50