如何在Angular中创建可观察的模拟来测试http rxjs retry / retryWhen?

aks*_*s94 5 jasmine rxjs angular

我的角度应用程序中有一个这种形式的http请求: this.http.get(url,options).pipe(retry(5))

我想使用茉莉花进行单元测试,以便http请求首先返回一个错误,但在随后的尝试中,返回期望的数据。

I previously returned http errors in this format as recommended by the angular.io documentation spyOn(http,'get').and.returnValue(defer(() => Promise.reject(errorObject)));

The rxjs operator retry does not seem to call the http.get function again, so changing the return value of the spy does not work. What I think I need to do is somehow return an observable from the spy which first emits the error and later emits the data. I thought about using a BehaviorSubject but don't think that accepts errors to be passed.

Is there any way of achieving this?

Dev*_*per 0

您可以尝试下面的方法吗?您还可以发布该函数的完整代码吗?

it('should check for the get error call ', () => {
            spyOn(http, 'get').and.returnValue(Observable.throw('error'));
            fixture.detectChanges() // or component.function()
            expect(http.get).toHaveBeenCalled();
        });
Run Code Online (Sandbox Code Playgroud)