Jasmine 存根函数导致类型错误“...不是函数”
Jasmine stubbed function results to an type error "... is not a function"
我正在尝试从我的测试中的“NavigationService”存根一个函数。
正如您在下面看到的,我想使用 NavigationServiceStub 而不是 NavigationService。 NavigationService 有一个名为“configureTargetScreensForNetworkConfiguration”的方法,我在存根中对其进行了模拟。
使用此设置我收到错误 TypeError: this.navigationService.configureTargetScreensForNetworkConfigurationis not a function。我错过了什么吗?
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();
}));
...
@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 {
}
...
}
由于存根是 class,我认为您应该使用 useClass
而不是 useValue
作为存根。
尝试:
providers: [
{provide: NetworkService, useValue: networkServiceSpy},
// change the line below to do useClass instead of useValue
{provide: NavigationService, useClass: NavigationServiceStub}
]
我正在尝试从我的测试中的“NavigationService”存根一个函数。
正如您在下面看到的,我想使用 NavigationServiceStub 而不是 NavigationService。 NavigationService 有一个名为“configureTargetScreensForNetworkConfiguration”的方法,我在存根中对其进行了模拟。
使用此设置我收到错误 TypeError: this.navigationService.configureTargetScreensForNetworkConfigurationis not a function。我错过了什么吗?
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();
}));
...
@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 {
}
...
}
由于存根是 class,我认为您应该使用 useClass
而不是 useValue
作为存根。
尝试:
providers: [
{provide: NetworkService, useValue: networkServiceSpy},
// change the line below to do useClass instead of useValue
{provide: NavigationService, useClass: NavigationServiceStub}
]