jeudi 30 janvier 2020

Jasmine Test Fails - But log suggests otherwise

This test is throwing the error Expected spy phone to have been called. - the mock class method is being called as demonstrated by the log in the terminal. The mock class is not being called by any other tests. I assume I am missing something simple here, but I am not sure why formatSpy in the test is failing.

My Provider in TestBed.ConfigureTestingModule:

        {provide: FormatService, useClass: FormatServiceMock},

My Mock Class:

        export class FormatServiceMock {
             phone() {
               console.log('format phone');
               return;
            }
       }

My Function Being Tested:

    public getPhone(): string {
        if (this.data && this.data.settings) {
            return this.format.phone(this.data.settings.phone);
        } else {
            return '';
        }
    }

My Test:

        it('should call format service', () => {
            service.data = data;
            service.getPhone();
            let formatSpy = spyOn(format, 'phone').and.callThrough();
            expect(formatSpy).toHaveBeenCalled();
        });

My Output:

LOG: 'format phone'
HeadlessChrome 79.0.3945 (Mac OS X 10.14.6): Executed 28 of 87 SUCCESS (0 secs / 1.477 secs)
LOG: 'format phone'
HeadlessChrome 79.0.3945 (Mac OS X 10.14.6) EnvironmentService getPhone should call format service FAILED
    Expected spy phone to have been called.

Aucun commentaire:

Enregistrer un commentaire