簡體   English   中英

如何在ng2中測試與業力的訂閱

[英]How to test subscribe with karma in ng2

從服務中,我得到一些信息流,因此在我的組件中,我得到這樣的信息:

this.service.getSome().subscribe(x => this.invokeHere(x));

在我的規格文件中,我有這樣的東西

beforeEach(() => {
    fixture = TestBed.createComponent(SomeComponent);
    component = fixture.componentInstance;

    service = fixture.debugElement.injector.get(Service);
    spy = spyOn(service, 'getSome')
        .and.returnValue(Observable.of(serviceStub));

    fixture.detectChanges();
});
it('test observable', inject([MockBackend], (mockBackend: MockBackend) => {
    mockBackend.connections.subscribe((connection: MockConnection) => {
      connection.mockError(new Error());
    });
    service.someRequest()
      .subscribe(() => { },(err) => {
        // your test
      });
  }));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM