无法读取未定义的属性(阅读' subscribe')在Ngoninit中使用单位测试

发布于 2025-01-25 12:27:08 字数 1729 浏览 1 评论 0原文

当前面对单元测试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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2025-02-01 12:27:08

Eventervice提供商中,添加Service属性CurrentEvents

providers: [
  {
    provide: EventsService, useValue: {
      getEvents: () => of([]),
      processEvents: () => of([]),
      currentEvents: of('events')
    }
  },
]

In the EventService provider, add the service property currentEvents too.

providers: [
  {
    provide: EventsService, useValue: {
      getEvents: () => of([]),
      processEvents: () => of([]),
      currentEvents: of('events')
    }
  },
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文