簡體   English   中英

Jasmine 測試 Observable 完成 Angular 中的 function

[英]Jasmine Test Observable complete function in Angular

我想在 Jasmine 中測試 rxjs Observable 的“完整”處理程序。

這是我要測試的代碼:

  doSomething()
  {
    const subscription = this.service.getObservable().subscribe(
        (next) => {
        //do something
        },
      (error) => {
        //handle error
      },
      () => {
        // do something on complete <== How can I test this function?

      }
    );
  }

我通過創建一個間諜為“下一個”處理程序編寫了測試:

const spy = spyOn<any>(service, 'getObservable').and.callFake(() => {
  return from(['nextValue']);
})

我通過創建一個引發錯誤的間諜為“錯誤”處理程序編寫了測試:

const spy = spyOn<any>(service, 'getObservable').and.callFake(() => {
  return throwError(new Error('error message'));
})

問題:如何模擬完整的處理程序?

這是您可以使用 jasmine 模擬完整處理程序的方法:

const spy = spyOn<any>(service, 'getObservable').and.callFake(() => {
      return new Observable(subscriber => {
        subscriber.complete();
      });
    });

暫無
暫無

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

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