Home > database >  Angular Jasmine testing - substring remove from number
Angular Jasmine testing - substring remove from number

Time:05-14

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.

CodePudding user response:

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');
});
  • Related