У меня есть служба / перехватчик, который возвращает Observable.Я вижу, loadOverlayService.start и next.handle оба были вызваны.Однако mockLoadOverlayService.popHttpRequest имеет 0 вызовов, записанных против него в шпионе.
Что я делаю неправильно?
interceptor.ts
@Injectable()
export class OverlayInterceptor implements HttpInterceptor {
constructor(
private loadOverlayService: LoadOverlayService) {
}
protected intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.loadOverlayService.start();
return next.handle(request)
.do(
// Start, do nothing
() => {},
// Error
() => this.loadOverlayService.stop(),
// Complete
() => this.loadOverlayService.stop()
);
}
}
interceptor.spec.ts
describe('OverlayInterceptor', () => {
let mockLoadOverlayService: any;
let mockHttpHandler: any;
beforeEach(() => {
mockLoadOverlayService = jasmine.createSpyObj('LoadOverlayService', ['pushHttpRequest', 'popHttpRequest']);
mockHttpHandler = jasmine.createSpyObj('HttpHandler', ['handle']);
TestBed.configureTestingModule({
providers: [
OverlayInterceptor,
{provide: LoadOverlayService, useValue: mockLoadOverlayService}
]
});
});
it('should show then hide modal', async(inject([OverlayInterceptor], (interceptor: OverlayInterceptor) => {
const request = new HttpRequest("GET", "someClientTypeCheckUri");
buildMocksForSuccessfulOutcome();
interceptor.intercept(request, mockHttpHandler).subscribe(res => {
expect(mockHttpHandler.handle).toHaveBeenCalledWith(request); // Works
expect(mockLoadOverlayService.pushHttpRequest.calls.count()).toBe(1, 'PushHttpRequest was called 1 times'); // Works
expect(mockLoadOverlayService.popHttpRequest.calls.count()).toBe(1, 'PopHttpRequest was called 1 times'); // Doesn't work
});
})));
function buildMocksForSuccessfulOutcome(): void {
mockHttpHandler.handle.and.returnValue(Observable.of(null));
}
});