Jasmine + Karma:ActivatedRoute 间谍不会被调用
Jasmine+Karma: ActivatedRoute spy not not getting called
我有一个使用 ActivateRoute
的 Angular 组件
getFlavorAndService(): void {
this._route.params.subscribe(params => {
const flavor: string = params.flavor;
const service: string = params.service;
const platform: string = params.platform;
if (platform) {
this.platform = platform;
} else {
this.platform = this._dataService.DEFAULT_PLATFORM;
}
if (flavor) {
this.flavor = flavor;
} else {
this.flavor = this._dataService.DEFAULT_FLAVOR;
}
if (service) {
this.service = service;
} else {
this.service = this._dataService.DEFAULT_SERVICE;
}
});
}
我正在尝试通过以下方式测试 getFlavorAndService()
方法:
it('Test', () => {
dataService.attributeSearchEnabled = false;
dataService.endpointSearchEnabled = false;
const spy = spyOn(activatedRoute, 'params').and.returnValue(of({
flavor: 'by-fund',
service: 'profile_benchmarks',
platform: 'on-prem',
}));
component.getFlavorAndService();
expect(spy).toHaveBeenCalled();
expect(component.platform).toBe('on-prem');
expect(component.service).toBe('profile_benchmarks');
expect(component.flavor).toBe('by-fund');
});
我还为 ActivatedRoute 创建了一个存根:
class ActivatedRouteStub {
get params() {
return of({});
}
get queryParams() {
return of({});
}
}
我的测试用例失败了,none 的期望工作正常。风味、服务和平台采用 class 中提供的默认值,而不是使用 spyOn(activatedRoute, 'params').and.returnValue(of({....
传递的值
我在TestBed.configureTestingModule
方法中也提供了ActivatedRouteStub
{ provide: ActivatedRoute, useClass: ActivatedRouteStub }
而不是:
const spy = spyOn(activatedRoute, 'params').and.returnValue(of({
尝试使用这个:
const spy = spyOnProperty(activatedRoute, 'params', 'get').and.returnValue(of({
spyOn -> spyOnProperty
查看文档 here。
我有一个使用 ActivateRoute
的 Angular 组件getFlavorAndService(): void {
this._route.params.subscribe(params => {
const flavor: string = params.flavor;
const service: string = params.service;
const platform: string = params.platform;
if (platform) {
this.platform = platform;
} else {
this.platform = this._dataService.DEFAULT_PLATFORM;
}
if (flavor) {
this.flavor = flavor;
} else {
this.flavor = this._dataService.DEFAULT_FLAVOR;
}
if (service) {
this.service = service;
} else {
this.service = this._dataService.DEFAULT_SERVICE;
}
});
}
我正在尝试通过以下方式测试 getFlavorAndService()
方法:
it('Test', () => {
dataService.attributeSearchEnabled = false;
dataService.endpointSearchEnabled = false;
const spy = spyOn(activatedRoute, 'params').and.returnValue(of({
flavor: 'by-fund',
service: 'profile_benchmarks',
platform: 'on-prem',
}));
component.getFlavorAndService();
expect(spy).toHaveBeenCalled();
expect(component.platform).toBe('on-prem');
expect(component.service).toBe('profile_benchmarks');
expect(component.flavor).toBe('by-fund');
});
我还为 ActivatedRoute 创建了一个存根:
class ActivatedRouteStub {
get params() {
return of({});
}
get queryParams() {
return of({});
}
}
我的测试用例失败了,none 的期望工作正常。风味、服务和平台采用 class 中提供的默认值,而不是使用 spyOn(activatedRoute, 'params').and.returnValue(of({....
我在TestBed.configureTestingModule
方法中也提供了ActivatedRouteStub
{ provide: ActivatedRoute, useClass: ActivatedRouteStub }
而不是:
const spy = spyOn(activatedRoute, 'params').and.returnValue(of({
尝试使用这个:
const spy = spyOnProperty(activatedRoute, 'params', 'get').and.returnValue(of({
spyOn -> spyOnProperty
查看文档 here。