Jasmine Unit Case 具有订阅的组件方法返回未定义并且总是失败

发布于 2025-01-11 22:28:17 字数 519 浏览 5 评论 0原文

我的组件具有以下方法:

get(): void {
  this.service.get().subscribe((res) => {
    this.response = res;
  });
}

我有以下测试用例:

it('should get content ', () => {
  const currentObject = {};

  spyOn(service, 'get').and.returnValue(of(currentObject));

  component.get();

  fixture.detectChanges();
  expect(component.response).toEqual(currentObject);

});

结果始终是:

预期未定义等于 Object({ })。

这里有很多与此相关的类似问题,但由于某种原因,这些问题都不适合我。

My component has the following method:

get(): void {
  this.service.get().subscribe((res) => {
    this.response = res;
  });
}

I have the following test case for it:

it('should get content ', () => {
  const currentObject = {};

  spyOn(service, 'get').and.returnValue(of(currentObject));

  component.get();

  fixture.detectChanges();
  expect(component.response).toEqual(currentObject);

});

The result is always:

Expected undefined to equal Object({ }).

There are plenty of similar questions regarding this here but for someone reason none of those work for me.

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

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

发布评论

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

评论(1

躲猫猫 2025-01-18 22:28:17

我认为这可能是由于代码的异步性质所致,其中 .subscribe 在您的 expect 之后发生。

尝试这样进行调试:

get(): void {
  this.service.get().subscribe((res) => {
    // !! add this log and ensure you see it first
    console.log('[get] This should happen first');
    this.response = res;
  });
}

// Test
it('should get content ', () => {
  const currentObject = {};

  spyOn(service, 'get').and.returnValue(of(currentObject));

  component.get();

  fixture.detectChanges();
  // !! add this log and ensure you see it last
  console.log('[test] This should happen last');
  expect(component.response).toEqual(currentObject);

});

确保您在这应该发生在最后之前看到这应该首先发生

我认为解决这个问题的一个潜在方法可能是使用fakeAsync/tick

// wrap callback function in fakeAsync
it('should get content ', fakeAsync(() => {
  const currentObject = {};

  spyOn(service, 'get').and.returnValue(of(currentObject));

  component.get();

  fixture.detectChanges();
  // call tick() here to ensure the asynchronous subscribe runs before the expect
  tick();
  expect(component.response).toEqual(currentObject);

}));

I am thinking this could be due to the asynchronous nature of the code where .subscribe happens after your expect.

Try this to debug:

get(): void {
  this.service.get().subscribe((res) => {
    // !! add this log and ensure you see it first
    console.log('[get] This should happen first');
    this.response = res;
  });
}

// Test
it('should get content ', () => {
  const currentObject = {};

  spyOn(service, 'get').and.returnValue(of(currentObject));

  component.get();

  fixture.detectChanges();
  // !! add this log and ensure you see it last
  console.log('[test] This should happen last');
  expect(component.response).toEqual(currentObject);

});

Make sure you see This should happen first before This should happen last.

I think a potential way to fix it is maybe using fakeAsync/tick.

// wrap callback function in fakeAsync
it('should get content ', fakeAsync(() => {
  const currentObject = {};

  spyOn(service, 'get').and.returnValue(of(currentObject));

  component.get();

  fixture.detectChanges();
  // call tick() here to ensure the asynchronous subscribe runs before the expect
  tick();
  expect(component.response).toEqual(currentObject);

}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文