茉莉花,角 - 如何测试特性主张?

发布于 2025-01-21 04:36:37 字数 1378 浏览 0 评论 0原文

茉莉花 - Angular

这是正在测试的组件的一部分(测试ThisingThisComppnent):

import { ThatComponent } from '.../shared/components';

export class TestingThisComppnent {
  (...)

  @ViewChild('child', { static: true })
  public map: GoogleMapComponent;

  public ngOnInit(): void { 
    (...)
    this.map.infoWindowComponent = ThatComponent;
  }
}

GoogleMapComponent:

export class GoogleMapComponent implements OnInit, OnDestroy, OnChanges {
 (...)
  @Input()
  public infoWindowComponent: any;
 (...)
}

如何测试这一行? -

this.map.infoWindowComponent = WorkItemInfoWindowComponent;

我们不是在项目中使用componentFixture,而是通过“ new”创建组件类 -

    subject = new WorkItemLocationTabComponent(
      firstSpy,
      secondSpy,
      thirdSpy,
      fourthSpy
    );

我尝试了创建间谍 -

infoWindowComponentSpy = spyOnProperty(subject.map, 'infoWindowComponent');

但是(我猜)“ InfowindowComponent”是一个@input(),我正在得到一个错误,属性“没有访问类型get”,因此我添加了get()和set() -

    Object.defineProperty(subject.map, 'infoWindowComponent', {
      get: function () {
          return this.value;
      },
      set: function (val) {
        this.value = val;
        return this.value;
      },
    });

但是,由于abfiets.map.infowdowcomponent的登录,测试仍然不起作用,始终返回“未定义” 。

Jasmine - Angular

This is a part of the component, that is being tested (TestingThisComppnent ):

import { ThatComponent } from '.../shared/components';

export class TestingThisComppnent {
  (...)

  @ViewChild('child', { static: true })
  public map: GoogleMapComponent;

  public ngOnInit(): void { 
    (...)
    this.map.infoWindowComponent = ThatComponent;
  }
}

GoogleMapComponent:

export class GoogleMapComponent implements OnInit, OnDestroy, OnChanges {
 (...)
  @Input()
  public infoWindowComponent: any;
 (...)
}

How can I test this line? -

this.map.infoWindowComponent = WorkItemInfoWindowComponent;

We are not using ComponentFixture in the project, but creating a component class by 'new' -

    subject = new WorkItemLocationTabComponent(
      firstSpy,
      secondSpy,
      thirdSpy,
      fourthSpy
    );

I've tried by creating a Spy -

infoWindowComponentSpy = spyOnProperty(subject.map, 'infoWindowComponent');

but because (I guess) 'infoWindowComponent' is an @Input(), I'm getting an error, that the property 'does not have access type get', so I've added get() and set() -

    Object.defineProperty(subject.map, 'infoWindowComponent', {
      get: function () {
          return this.value;
      },
      set: function (val) {
        this.value = val;
        return this.value;
      },
    });

But still, the test doesn't work, because log of subject.map.infoWindowComponent always returns 'undefined'.

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

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

发布评论

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

评论(1

妄司 2025-01-28 04:36:37

由于您不使用testbed,并且仅执行

subject = new WorkItemLocationTabComponent(
      firstSpy,
      secondSpy,
      thirdSpy,
      fourthSpy
    );

ViewChild将永远无法填充MAP变量。

尝试这样做:

it('sets the infoWindowComponent', () => {
  (subject.map as any) = { infoWindowComponent: null }
  subject.ngOnInit();
  expect((subject.map as any).infoWindowComponent).not.toBeNull();
});

Since you're not using TestBed and just doing

subject = new WorkItemLocationTabComponent(
      firstSpy,
      secondSpy,
      thirdSpy,
      fourthSpy
    );

ViewChild will never be able to populate the map variable.

Try doing this:

it('sets the infoWindowComponent', () => {
  (subject.map as any) = { infoWindowComponent: null }
  subject.ngOnInit();
  expect((subject.map as any).infoWindowComponent).not.toBeNull();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文