mardi 11 septembre 2018

Unit test does not pass even though it obviously should?

I have a simple function that I am wanting to test, however the obvious outcome is not occurring...

  1. I pass a string into my function
  2. If it matches with an element of my array
    1. Returns the string
  3. If it does not match an element of my array
    1. returns string 'default'

My Component

const exceptionalRaceStatuses = [
   {name: 'hare-failure'},
   {name: 'trap-failure'}
];

isStatusExceptional(currentStatus): string {
    for (const status of this.exceptionalRaceStatuses) {
      if (status.name === currentStatus) {
        return currentStatus;
      }
    }
    return 'default';
  }

My Test

beforeEach(async(() => {

    TestBed.configureTestingModule({
      declarations: [EventComponent, ConfirmationComponent],
      imports: [ReactiveFormsModule, FormsModule],
      providers: []
    });

    fixture = TestBed.createComponent(EventComponent);
    component = fixture.componentInstance;

    mockEventService.getById.and.returnValue(of(event));
    mockStatusValidation.getStatusValidation.and.returnValue(of(statuses));

    component.ngOnInit();
  }));

it('should return current status if it is part of exceptional statuses', () => {
    const returned = component.isStatusExceptional('hare-failure');
    expect(returned).toEqual('hare-failure');
  });

Aucun commentaire:

Enregistrer un commentaire