Angular 如何使用jasmine Mock对rxjs tap运算符的错误块进行单元测试?
考虑以下Http拦截器:Angular 如何使用jasmine Mock对rxjs tap运算符的错误块进行单元测试?,angular,rxjs,karma-jasmine,karma-runner,rxjs-pipeable-operators,Angular,Rxjs,Karma Jasmine,Karma Runner,Rxjs Pipeable Operators,考虑以下Http拦截器: export class BusyIndicatorInterceptor implements HttpInterceptor { constructor(private busyIndicatorService: BusyIndicatorService) { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>
export class BusyIndicatorInterceptor implements HttpInterceptor {
constructor(private busyIndicatorService: BusyIndicatorService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.busyIndicatorService.show();
return next.handle(req).pipe(tap((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
this.busyIndicatorService.hide();
}
}, err => {
console.warn(err);
this.busyIndicatorService.hide();
}));
}
}
结果:
Expected spy BusyIndicatorService.hide to have been called once. It was called 0 times
下面的测试用于测试success块,其中我模拟处理程序以返回新的HttpResponse
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpResponse()));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
以下是完整的测试类:
describe("BusyIndicatorInterceptor", () => {
let testee: BusyIndicatorInterceptor;
let busyIndicatorService: any;
let mockRequest: any;
let mockhandler: any;
beforeEach(() => {
mockRequest = jasmine.createSpyObj("HttpRequest", [""]);
mockhandler = jasmine.createSpyObj("HttpHandler", ["handle"]);
busyIndicatorService = jasmine.createSpyObj("BusyIndicatorService", ["show", "hide"]);
busyIndicatorService.visibility = jasmine.createSpyObj("BehaviourSubject<boolean>", ["next"]);
testee = new BusyIndicatorInterceptor(busyIndicatorService);
});
it("should be created", () => {
expect(testee).toBeTruthy();
});
describe("intercept", () => {
it("should set busy indicator visibility to true", () => {
mockhandler.handle.and.returnValue(of(null));
testee.intercept(mockRequest, mockhandler);
expect(busyIndicatorService.show).toHaveBeenCalledTimes(1);
});
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpResponse()));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpErrorResponse({ error: "Not Found", status: 404 })));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
});
});
description(“总线指示接收器”),()=>{
让受试者:总线指示接收器;
让BusyIndicator服务:任何;
让我们请求:任何;
让我们看看:任何;
在每个之前(()=>{
mockRequest=jasmine.createSpyObj(“HttpRequest”,[“”]);
mockhandler=jasmine.createSpyObj(“HttpHandler”,[“handle”]);
busyIndicatorService=jasmine.createSpyObj(“busyIndicatorService”[“show”,“hide”]);
busyIndicatorService.visibility=jasmine.createSpyObj(“行为主体”[“下一步]);
受试者=新的总线指示接收器(总线指示服务);
});
它(“应该创建”,()=>{
expect(testee.toBeTruthy();
});
描述(“截取”,()=>{
它(“应将繁忙指示器的可见性设置为true”,()=>{
mockhandler.handle.and.returnValue(of(null));
拦截(mockRequest、mockhandler);
期望(busyIndicatorService.show)。已被催收时间(1);
});
它(“当返回HttpResponse时,应将忙碌指示器可见性设置为false”,()=>{
mockhandler.handle.and.returnValue(新HttpResponse()的);
const result=testee.intercept(mockRequest,mockhandler);
结果。订阅(()=>{
期望(busyIndicatorService.hide).已被调用时间(1);
});
});
它(“当返回HttpResponse时,应将忙碌指示器可见性设置为false”,()=>{
mockhandler.handle.and.returnValue(属于(新的HttpErrorResponse({错误:“未找到”,状态:404}));
const result=testee.intercept(mockRequest,mockhandler);
结果。订阅(()=>{
期望(busyIndicatorService.hide).已被调用时间(1);
});
});
});
});
您必须注入DoneFN并在订阅中调用它。。如果有帮助的话…@AndréRoggeriCampos你有例子吗?@Maryannah当我有一个tap操作符时,你似乎在使用.catch
,我需要模拟错误。你有更具体的例子吗?@uex隐含的要点是让你使用catch
,而不是tap操作符的第二次回调(基本上也是这样),你必须注入DoneFN并在订阅中调用它。。如果有帮助的话…@AndréRoggeriCampos你有例子吗?@Maryannah当我有一个tap操作符时,你似乎在使用.catch
,我需要模拟错误。你有更具体的例子吗?@uex隐含的要点是让你使用catch
,而不是tap操作符的第二次回调(基本上也是这样)
describe("BusyIndicatorInterceptor", () => {
let testee: BusyIndicatorInterceptor;
let busyIndicatorService: any;
let mockRequest: any;
let mockhandler: any;
beforeEach(() => {
mockRequest = jasmine.createSpyObj("HttpRequest", [""]);
mockhandler = jasmine.createSpyObj("HttpHandler", ["handle"]);
busyIndicatorService = jasmine.createSpyObj("BusyIndicatorService", ["show", "hide"]);
busyIndicatorService.visibility = jasmine.createSpyObj("BehaviourSubject<boolean>", ["next"]);
testee = new BusyIndicatorInterceptor(busyIndicatorService);
});
it("should be created", () => {
expect(testee).toBeTruthy();
});
describe("intercept", () => {
it("should set busy indicator visibility to true", () => {
mockhandler.handle.and.returnValue(of(null));
testee.intercept(mockRequest, mockhandler);
expect(busyIndicatorService.show).toHaveBeenCalledTimes(1);
});
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpResponse()));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
it("should set busy indicator visibility to false when HttpResponse is returned", () => {
mockhandler.handle.and.returnValue(of(new HttpErrorResponse({ error: "Not Found", status: 404 })));
const result = testee.intercept(mockRequest, mockhandler);
result.subscribe(() => {
expect(busyIndicatorService.hide).toHaveBeenCalledTimes(1);
});
});
});
});