无法使用单元测试读取 ngOnInit 中订阅的未定义(读取 'subscribe')的属性
Cannot read properties of undefined (reading 'subscribe') of subscribe inside ngOnInit with unit test
当前面临单元测试错误:
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.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');
});
在 EventService
提供程序中,将服务 属性 添加到 currentEvents
。
providers: [
{
provide: EventsService, useValue: {
getEvents: () => of([]),
processEvents: () => of([]),
currentEvents: of('events')
}
},
]
当前面临单元测试错误:
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.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');
});
在 EventService
提供程序中,将服务 属性 添加到 currentEvents
。
providers: [
{
provide: EventsService, useValue: {
getEvents: () => of([]),
processEvents: () => of([]),
currentEvents: of('events')
}
},
]