简体   繁体   English

从服务订阅可观察时,角度单元测试管道不是功能

[英]Angular unit test pipe is not a function when subscribe observable from a service

I'm writing a unit test to subscribe observable from a service.我正在编写一个单元测试来从服务中订阅 observable。 I'm getting pipe is not a function error.我得到pipe is not a function错误。 I have created a jest spy for fundsListByClientId$ and simply returned the mock data.我为fundsListByClientId$创建了一个笑话间谍,并简单地返回了模拟数据。 Please help me to know what I have missed?请帮助我知道我错过了什么? I'm using Jest for unit test我正在使用 Jest 进行单元测试

Error:错误:

TypeError: this._fundsService.fundsListByClientId$.pipe is not a function

fund.spec.ts基金.spec.ts

let service: FundsService;

beforeEach(async () => {
  await TestBed.configureTestingModule({
    providers: [
      {
        provide: FundsService,
        useValue: {
          fundsListByClientId$: jest.fn(),
        },
      },
    ],
  }).compileComponents();
});

beforeEach(() => {
  service = TestBed.inject(FundsService);
});

it("Should sort fundlist object", () => {
  const mockResponse = {cart: false, name: "test" };
  const spyCopmponent = jest.spyOn(component, "onFundsByClientId");
  jest
    .spyOn(service, "fundsListByClientId$", "get")
    .mockReturnValue(of(mockResponse));

  component.onFundsByClientId();

  service.fundsListByClientId$.subscribe((result) => {
    expect(result).toEqual(mockResponse);
  });

  expect(spyCopmponent).toHaveBeenCalled();
});

fund.component.ts基金组件.ts

 onFundsByClientId() {
        this._fundsService.fundsListByClientId$
            .pipe(takeUntil(this._unsubscribeAll))
            .subscribe((fundList: Fund[]) => {
                this.fundList = fundList.sort((a, b) => {
                    if (a.fund_status === 'I' && b.fund_status !== 'I') return 1;
                    if (a.fund_status !== 'I' && b.fund_status === 'I') return -1;
                    return a.fund_name > b.fund_name ? 1 : -1;
                });
                this._changeDetectorRef.markForCheck();
            });
    }

fund.service.ts基金服务.ts

private _fundList: BehaviorSubject<Fund[] | null> = new BehaviorSubject(null);

get fundsListByClientId$(): Observable<Fund[]> {
  return this._fundList.asObservable();
}

getFundsListByClientId(clientId: string, filters?: any): Observable<Fund[]> {
        const queryParams: HttpParams = UtilsService.buildQueryParams(filters);
        return this.http
            .get(this.endpoint, environment.fundApi + `/fund`, queryParams)
            .pipe(
                tap((response) => {
                    this._fundList.next(response);
                }),
            );
    }

Try changing the mock to this:尝试将模拟更改为:

{
        provide: FundsService,
        useValue: {
          fundsListByClientId$: of({cart: false, name: "test" }),
        },
      },

And get rid of this:并摆脱这个:

jest
    .spyOn(service, "fundsListByClientId$", "get")
    .mockReturnValue(of(mockResponse));

And see if it works.看看它是否有效。

I am not familiar with Jest but the way you mocked it with a jest.fn() and using spyOn on the get might not work.我不熟悉 Jest,但你用jest.fn()模拟它并在get上使用spyOn的方式可能不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM