Angular Jasmine stubbed函数导致类型错误“;。。。不是一个函数;

Angular Jasmine stubbed函数导致类型错误“;。。。不是一个函数;,angular,jasmine,karma-jasmine,Angular,Jasmine,Karma Jasmine,我试图在测试中从“NavigationService”存根一个函数 正如您在下面看到的,我想使用NavigationServiceStub而不是NavigationService。NavigationService有一个名为“configureTargetScreensForNetworkConfiguration”的方法,我在存根中模拟了该方法 通过此设置,我得到错误类型错误:this.navigationService.configureTargetScreensfornetworkconf

我试图在测试中从“NavigationService”存根一个函数

正如您在下面看到的,我想使用NavigationServiceStub而不是NavigationService。NavigationService有一个名为“configureTargetScreensForNetworkConfiguration”的方法,我在存根中模拟了该方法

通过此设置,我得到错误类型错误:this.navigationService.configureTargetScreensfornetworkconfiguration不是一个函数。我错过什么了吗

describe('NetworkDevicesComponent', () => {
...
    beforeEach(waitForAsync(() => {
        networkServiceSpy = jasmine.createSpyObj('NetworkService', ['getNetworkDevices']);
        networkServiceSpy.getNetworkDevices.and.returnValue(
            of([
                {type: NetworkType.WLAN, state: NetworkState.CONNECTING, links: null}
            ])
        );


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

由于存根是一个类,我认为应该使用
useClass
而不是
useValue
作为存根

尝试:

@Injectable()
export class NavigationService {
...

    public configureTargetScreensForNetworkConfiguration(params: ParamMap): void {
        if (this.hasNetworkTargetScreenParams(params)) {
            this.setTargetScreensForNetworkConfiguration(params.get('backwardUrl'), params.get('forwardUrl'));
            this.staticNetworkNavigationEnabled = true;
        } else {
            this.setTargetScreensForNetworkConfiguration('../../', '../../network-devices');
            this.staticNetworkNavigationEnabled = false;
        }

        console.log('configured network-setting urls with backwardUrl:', this.targetScreenBeforeNetworkConfiguration,
            'forwardUrl:', this.targetScreenAfterNetworkConfiguration,
            'static-routing:', this.staticNetworkNavigationEnabled);

    }
...
}
export class NavigationServiceStub {

    configureTargetScreensForNetworkConfiguration(params: ParamMap): void {
    }
...
}
providers: [
                {provide: NetworkService, useValue: networkServiceSpy},
                // change the line below to do useClass instead of useValue
                {provide: NavigationService, useClass: NavigationServiceStub}
            ]