【问题标题】:how can i test angular acrivated route subscribe together with snapshot data?我如何测试角度激活路由订阅和快照数据?
【发布时间】:2022-01-11 20:36:59
【问题描述】:

我有代码,我订阅了 activateRoute.url 并且在订阅中我访问了 sanpshot 数据。 我为 activateRoute 创建了模拟,但我的测试一直失败。

这是测试代码

This is the code i want to test

This is the test

我试图在没有订阅的情况下更改代码并且它有效。 但是,当我尝试在订阅中执行此操作时,测试失败了。

【问题讨论】:

  • 请复制/粘贴代码而不是显示为图片

标签: angular unit-testing jasmine karma-jasmine angular-unit-test


【解决方案1】:

我认为您应该将您的测试封装在 fakeAsync 中并调用 tick() 以等待订阅中的代码。

it('should wait', fakeAsync(() => {
  ...
  component.ngAfterViewInit();
  tick();
  expect(something);
}));

【讨论】:

  • 它也不起作用?
  • 你能分享一下新的测试来帮助你吗? (作为代码,而不是图像)。如果你能分享一个小代码沙箱或回购会更好!
  • 我会的。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 2019-08-26
  • 2020-08-14
  • 1970-01-01
相关资源
最近更新 更多