Giuseppe
Giuseppe

Reputation: 375

Jasmine stubbed function results to an type error "... is not a function"

I'm trying to stub a function from a "NavigationService" inside my test.

As you can see below, instead of the NavigationService I want to use my NavigationServiceStub. NavigationService has a method named "configureRoutedScreens" which i mocked in the stub.

With this setup im getting the Error TypeError: this.navigationService.configureRoutedScreens is not a function. Am I missing something?

describe('NetworkInterfacesComponent', () => {
...
    beforeEach(waitForAsync(() => {

        TestBed.configureTestingModule({
            declarations: [
                NetworkInterfacesComponent
            ],
            imports: [
                RouterTestingModule
            ],
            providers: [
                {provide: NetworkService, useValue: networkServiceSpy},
                {provide: NavigationService, useValue: NavigationServiceStub}
            ]
        }).compileComponents();
    }));
...
export class NavigationServiceStub {

    configureRoutedScreens(params: ParamMap): void {
    }
...
}

Upvotes: 3

Views: 367

Answers (1)

AliF50
AliF50

Reputation: 18809

Since the stub is a class, I am thinking you should use useClass instead of useValue for the stub.

Try:

providers: [
                {provide: NetworkService, useValue: networkServiceSpy},
                // change the line below to do useClass instead of useValue
                {provide: NavigationService, useClass: NavigationServiceStub}
            ]

Upvotes: 1

Related Questions