【问题标题】:Angular async detect changes角度异步检测更改
【发布时间】:2017-12-28 15:14:52
【问题描述】:

如何在没有测试平台的情况下在测试 Angular 时“检测变化”?

有了一个组件,我就会有这个:

it('should work', async(() => {
  const fixture = TestBed.createComponent(TestComponent);
  // do some async call
  fixture.detectChanges();   // <=== This is what I want
  expect(something).toEqual(somethingElse);
});

对于服务,我并不总是有一个测试平台。如何在断言之前告诉角度等待?示例:

it('should work', async(() => {
  const service = new MyService();
  const sub = service.emitter$.subscribe((val) => {
    // expect(val).toEqual(otherVal);
  });
  service.doAsyncWork();
});

如何将这些连接起来?如果我有两个阶段要检查(例如,在异步工作之前一次,之后一次,但都在某个初始阶段之后)怎么办?

【问题讨论】:

    标签: angular asynchronous testing


    【解决方案1】:

    使用fakeAsynctick 函数来模拟时间段。

    it('should work', fakeAsync(() => {
      const fixture = TestBed.createComponent(TestComponent);
      // do some async call
      tick(10000);    
      expect(something).toEqual(somethingElse);
    });
    

    【讨论】:

    • 这是一个临时解决方案,在某些情况下,我依赖于我无法控制的东西来获取这些传入数据。不过还是谢谢。
    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 2019-11-05
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多