Angular 无法在ngOnInit()中测试订阅
我正在使用Observable进行事件订阅,但无法对其进行测试 这是我的密码 服务Angular 无法在ngOnInit()中测试订阅,angular,unit-testing,jasmine,Angular,Unit Testing,Jasmine,我正在使用Observable进行事件订阅,但无法对其进行测试 这是我的密码 服务 export class EventService<T> { private eventSubject = new Subject<string>(); eventSubject$ = this.eventSubject.asObservable(); dispathEvent(name) { this.eventSubject.next(name)
export class EventService<T> {
private eventSubject = new Subject<string>();
eventSubject$ = this.eventSubject.asObservable();
dispathEvent(name) {
this.eventSubject.next(name);
}
}
let mockEventService :any;
mockEventService = {
eventSubject: jasmine.createSpyObj('eventSubject', ['next']),
eventSubject$: jasmine.createSpyObj('eventSubject$', ['subscribe']),
};
//some code
providers: [{ provide: EventService, useValue: mockEventService }]
// some code
fixture = TestBed.createComponent(UserComponent);
component = new TeamOvertimeComponent(mockEventService ,mockOtherService);
....
it('should test ngOnInt Service var status', () => {
expect(mockEventService.eventSubject$['subscribe']).toHaveBeenCalled()
fixture.detectChanges();
//
component.ngOnInit();
});
规范文件
export class EventService<T> {
private eventSubject = new Subject<string>();
eventSubject$ = this.eventSubject.asObservable();
dispathEvent(name) {
this.eventSubject.next(name);
}
}
let mockEventService :any;
mockEventService = {
eventSubject: jasmine.createSpyObj('eventSubject', ['next']),
eventSubject$: jasmine.createSpyObj('eventSubject$', ['subscribe']),
};
//some code
providers: [{ provide: EventService, useValue: mockEventService }]
// some code
fixture = TestBed.createComponent(UserComponent);
component = new TeamOvertimeComponent(mockEventService ,mockOtherService);
....
it('should test ngOnInt Service var status', () => {
expect(mockEventService.eventSubject$['subscribe']).toHaveBeenCalled()
fixture.detectChanges();
//
component.ngOnInit();
});
我认为这是一个问题
TypeError: this.service.eventSubject$.subscribe is not a function
在每次发布之前,我都尝试将它们保留在中,但问题仍然存在您需要从具有
subscribe
方法的对象创建一个间谍,在本例中,可观察到:
mockEventService = {
eventSubject: jasmine.createSpyObj('eventSubject', ['next']),
eventSubject$: jasmine.createSpyObj('eventSubject$', ['subscribe']),
};
在我看来,其余的代码可以保持不变。你可以这样监视它:
expect(mockOvertimeService.eventSubject$['subscribe']).toHaveBeenCalled()
要测试subscribe块,可以执行以下操作(未测试的代码):
您需要从具有
subscribe
方法的对象创建一个间谍,在这种情况下,可观察到:
mockEventService = {
eventSubject: jasmine.createSpyObj('eventSubject', ['next']),
eventSubject$: jasmine.createSpyObj('eventSubject$', ['subscribe']),
};
在我看来,其余的代码可以保持不变。你可以这样监视它:
expect(mockOvertimeService.eventSubject$['subscribe']).toHaveBeenCalled()
要测试subscribe块,可以执行以下操作(未测试的代码):
现在,我得到的错误是
TypeError:无法读取spec文件中mockOvertimeService.eventSubject$.and.returnValue(of('string'))中未定义的
的属性'returnValue',
@kartheks如果你在模拟整个东西,为什么要查看返回值:)?您只需使用toHaveBeenCalled()
检查是否调用了它,我尝试了toHaveBeenCalled()
但仍然得到了错误,如error::应该是间谍,但得到了对象({subscribe:spy on widgetActiveSource$.subscribe})。用法:expect()。然后,您不应该使用createSpyObj
,而是在创建的observableNow上使用createSpy,我得到的错误是TypeError:undefined
的属性'returnValue',显示在mockOvertimeService.eventSubject$.and.returnValue(of('string'))
@Kartheek您正在模拟整个事情,为什么要查看返回值:)?您只需使用toHaveBeenCalled()
检查是否调用了它,我尝试了toHaveBeenCalled()
但仍然得到了错误,如error::应该是间谍,但得到了对象({subscribe:spy on widgetActiveSource$.subscribe})。用法:expect()。然后,您不应该使用createSpyObj
,而是在创建的可观察对象上使用createSpy