gpt4 book ai didi

angular - Jasmine stub 函数导致类型错误 "... is not a function"

转载 作者:行者123 更新时间:2023-12-04 03:46:29 26 4
gpt4 key购买 nike

我正在尝试从我的测试中的“NavigationService” stub 一个函数。

正如您在下面看到的,我想使用 NavigationServiceStub 而不是 NavigationService。 NavigationService 有一个名为“configureRoutedScreens”的方法,我在 stub 中对其进行了模拟。

通过此设置,我收到错误 TypeError: this.navigationService.configureRoutedScreens is not a function。我错过了什么吗?

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 {
}
...
}

最佳答案

由于 stub 是一个类,我认为您应该为 stub 使用 useClass 而不是 useValue

尝试:

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

关于angular - Jasmine stub 函数导致类型错误 "... is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65073045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com