Dan*_*ley 6 unit-testing jasmine typescript angular
我一直在尝试为我的 Angular 组件编写单元测试。目前,在我的服务调用中获取组件的数据时,我有一个可观察的值,一旦调用完成,该可观察值就会被赋予 true 。在我的组件中订阅了这个可观察对象,以便组件知道调用何时完成。我已经成功地模拟了组件中对数据的调用,但我正在努力寻找一种方法来模拟单个可观察值。
我能找到的关于 SO 的所有问题都是关于模拟组件中服务的函数调用,但我能找到的所有问题都不是关于模拟单个可观察的。
这是我在服务中的函数调用。正如您所看到的,一旦函数运行,可观察量就会被赋予一个新值finalize
:
public getSmallInfoPanel(key: string): BehaviorSubject<InfoPanelResponse> {
if (key) {
this.infoPanel = new BehaviorSubject<InfoPanelResponse>(null);
this.http.get(`${this.apiUrl}api/Panels/GetInfoPanel/${key}`).pipe(
retry(3),
finalize(() => {
this.hasLoadedSubject.next(true);
}))
.subscribe((x: InfoPanelResponse) => this.infoPanel.next(x));
}
return this.infoPanel;
}
Run Code Online (Sandbox Code Playgroud)
Observable
这是我在服务中创建的方法:
private hasLoadedSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
public hasLoadedObs: Observable<boolean> = this.hasLoadedSubject.asObservable();
Run Code Online (Sandbox Code Playgroud)
然后在我的组件中,我订阅了Observable
从以下位置创建的内容BehaviourSubject
:
public hasLoaded: boolean;
ngOnInit() {
this.infoPanelSmallService.hasLoadedObs.subscribe(z => this.hasLoaded = z);
}
Run Code Online (Sandbox Code Playgroud)
当我运行ng test
组件测试失败时,因为它不知道是什么,hasLoadedObs
所以它无法订阅它。
如果我可以提供更多信息,请告诉我。谢谢。
更新1
describe('InformationPanelSmallComponent', () => {
let component: InformationPanelSmallComponent;
let fixture: ComponentFixture<InformationPanelSmallComponent>;
let mockInfoPanelService;
let mockInfoPanel: InfoPanel;
let mockInfoPanelResponse: InfoPanelResponse;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FontAwesomeModule,
HttpClientTestingModule
],
declarations: [InformationPanelSmallComponent, CmsInfoDirective],
providers: [
{ provide: InfoPanelSmallService, useValue: mockInfoPanelService }
]
})
.compileComponents();
}));
beforeEach(() => {
mockInfoPanel = {
Title: 'some title',
Heading: 'some heading',
Description: 'some description',
ButtonText: 'some button text',
ButtonUrl: 'some button url',
ImageUrl: 'some image url',
Key: 'test-key',
SearchUrl: '',
VideoUrl: ''
}
mockInfoPanelResponse = {
InfoPanel: mockInfoPanel
}
fixture = TestBed.createComponent(InformationPanelSmallComponent);
component = fixture.componentInstance;
mockInfoPanelService = jasmine.createSpyObj(['getSmallInfoPanel']);
component = new InformationPanelSmallComponent(mockInfoPanelService);
component.key = "test-key"
});
it('should create', () => {
expect(component).toBeTruthy();
});
//TO DO
it('should get info panel from info panel service', () => {
mockInfoPanelService.getSmallInfoPanel.and.returnValue(of(mockInfoPanelResponse));
component.ngOnInit();
expect(mockInfoPanelService.getSmallInfoPanel).toHaveBeenCalled();
expect(component.infoPanel).toEqual(mockInfoPanel);
});
});
Run Code Online (Sandbox Code Playgroud)
我发现这与我模拟服务和创建组件的顺序有关。我还使用了TestBed.overrideProvider
与上面使用的不同的东西。这是最终生成的测试文件:
describe('InformationPanelSmallComponent', () => {
let component: InformationPanelSmallComponent;
let fixture: ComponentFixture<InformationPanelSmallComponent>;
let mockInfoPanelService;
let mockInfoPanel: InfoPanel;
let mockInfoPanelResponse: InfoPanelResponse;
beforeEach(async(() => {
mockInfoPanelService = jasmine.createSpyObj(['getSmallInfoPanel', 'hasLoadedObs']);
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
FontAwesomeModule,
HttpClientTestingModule
],
declarations: [InformationPanelSmallComponent, CmsInfoDirective, UrlRedirectDirective],
providers: [
{ provide: 'BASE_URL', useValue: '/' },
{ provide: 'API_URL', useValue: '/' }
]
})
TestBed.overrideProvider(InfoPanelSmallService, { useValue: mockInfoPanelService });
TestBed.compileComponents();
}));
beforeEach(() => {
mockInfoPanel = {
Title: 'some title',
Heading: 'some heading',
Description: 'some description',
ButtonText: 'some button text',
ButtonUrl: 'some button url',
ImageUrl: 'some image url',
Key: 'test-key',
SearchUrl: '',
VideoUrl: ''
}
mockInfoPanelResponse = {
InfoPanel: mockInfoPanel
}
fixture = TestBed.createComponent(InformationPanelSmallComponent);
component = fixture.componentInstance;
mockInfoPanelService.getSmallInfoPanel.and.returnValue(of(mockInfoPanelResponse));
mockInfoPanelService.hasLoadedObs = of(true);
component.key = "test-key"
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('ngOnInit', () => {
it('should get info panel from info panel service', () => {
expect(component.hasLoaded).toEqual(true);
expect(mockInfoPanelService.getSmallInfoPanel).toHaveBeenCalled();
expect(component.infoPanel).toEqual(mockInfoPanel);
});
it('should get loaded is true from service', () => {
expect(component.hasLoaded).toEqual(true);
});
});
});
Run Code Online (Sandbox Code Playgroud)
然后我没有再遇到任何错误,并且测试实际上运行正常。感谢@RuiMarques 和其他人提供的所有意见。