无法读取未定义的属性(阅读' subscribe')在Ngoninit中使用单位测试
当前面对单元测试errror:
AppComponent > should call app
TypeError: Cannot read properties of undefined (reading 'subscribe')
在我的ngoninit内部,我有:
this.eventsService.currentEvents.subscribe(events => {
this.events = events;
return this.processEvents(this.events);
});
在这里失败的测试(ngoninit被称为):
it(`should call app`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
app.ngOnInit();
});
我的活动服务是:
export class EventsService {
constructor(private http: HttpClient, private store: Store<AppState>) {}
private eventsSource = new BehaviorSubject<any>([]);
currentEvents: Observable<any> = this.eventsSource .asObservable();
getEvents(events: []) {
this.eventsSource.next(events);
}
}
app.component.spent.spec也设置了此设置:
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientModule],
declarations: [AppComponent],
providers: [
{ provide: EventsService , useValue: {
getEvents: () => of([]),
processEvents: () => of([])
}},
provideMockStore({}),
{ provide: Angulartics2, useValue: {} },
{ provide: Angulartics2GoogleAnalytics, useValue: { startTracking: () => {}} },
{ provide: WindowRef }
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
eventService = TestBed.inject(EventsService);
(<any>window).ga = jasmine.createSpy('ga');
});
Currently facing a unit test errror:
AppComponent > should call app
TypeError: Cannot read properties of undefined (reading 'subscribe')
Inside my ngOnInit I have:
this.eventsService.currentEvents.subscribe(events => {
this.events = events;
return this.processEvents(this.events);
});
The test where it fails is here (where ngOnInit is called):
it(`should call app`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
app.ngOnInit();
});
And my events service is:
export class EventsService {
constructor(private http: HttpClient, private store: Store<AppState>) {}
private eventsSource = new BehaviorSubject<any>([]);
currentEvents: Observable<any> = this.eventsSource .asObservable();
getEvents(events: []) {
this.eventsSource.next(events);
}
}
The app.component.spec has this set up as well:
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientModule],
declarations: [AppComponent],
providers: [
{ provide: EventsService , useValue: {
getEvents: () => of([]),
processEvents: () => of([])
}},
provideMockStore({}),
{ provide: Angulartics2, useValue: {} },
{ provide: Angulartics2GoogleAnalytics, useValue: { startTracking: () => {}} },
{ provide: WindowRef }
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
eventService = TestBed.inject(EventsService);
(<any>window).ga = jasmine.createSpy('ga');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Eventervice
提供商中,添加Service属性CurrentEvents
。In the
EventService
provider, add the service propertycurrentEvents
too.