测试在角度的管道内的地图(隔离测试)

发布于 2025-02-12 20:30:42 字数 894 浏览 0 评论 0原文

我有一种带有图像的管道的方法。当我运行测试时,我找不到进入该地图的方法。

这是我的组件代码

public getParents(): void {
    Eif (this.filter.siteId !== null) {
      this.parents$ = this.parentsM().pipe(
        map(items => items.data),
        catchError(error => {
        this.messageService.errorHandler(error);
        return Array<any>();
      }));
    }
  }

it("getParents with siteId", async(() => {
      const mockItems:Response = {
      codError: "",
      msgError: "",
      data: "test"}
      let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
    component.getParents();
    component.parentsM().subscribe(items => {
      expect(items).toEqual(mockItems);
    });
    spy.calls.reset();
  }));

这是我的规格。

。 alt =“管道地图测试”>

I have a method that has a pipe with a map inside of it. When I run the test I can't find a way to get into that map.

This is my component code

public getParents(): void {
    Eif (this.filter.siteId !== null) {
      this.parents$ = this.parentsM().pipe(
        map(items => items.data),
        catchError(error => {
        this.messageService.errorHandler(error);
        return Array<any>();
      }));
    }
  }

This is my Spec.ts code

it("getParents with siteId", async(() => {
      const mockItems:Response = {
      codError: "",
      msgError: "",
      data: "test"}
      let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
    component.getParents();
    component.parentsM().subscribe(items => {
      expect(items).toEqual(mockItems);
    });
    spy.calls.reset();
  }));

This is my code coverage

Pipe map test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙与沫 2025-02-19 20:30:43

这里有多个问题。实际上,您必须订阅this.parents $才能执行地图,这使您的测试异步。在您的测试中,您正在订阅parentsm()的结果,我猜这不会返回this.parents $,因此您不订阅你写的烟斗。您也可以使用Waitforasync而不是async

请尝试以下操作:

it("getParents with siteId", waitForAsync(() => { // <== waitForAsync
      const mockItems:Response = {
      codError: "",
      msgError: "",
      data: "test"}
      let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
    component.getParents();
    component.parents$.subscribe(items => { // <== parents$
      expect(items).toEqual(mockItems);
    });
    
    spy.calls.reset();
}));

Multiple issues here. You actually have to subscribe to this.parents$ to execute the map, which makes your test async. In your test, you're subscribing on the result of parentsM(), which I guess doesn't return exactly this.parents$, so you're not subscribing to the pipe you wrote. You may also use waitForAsync instead of async.

Please try this:

it("getParents with siteId", waitForAsync(() => { // <== waitForAsync
      const mockItems:Response = {
      codError: "",
      msgError: "",
      data: "test"}
      let spy = spyOn<any>(component, "parentsM").and.returnValue(scheduled([mockItems], asyncScheduler));
    component.getParents();
    component.parents$.subscribe(items => { // <== parents$
      expect(items).toEqual(mockItems);
    });
    
    spy.calls.reset();
}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文