的角度导向演示通过调用新服务(),并且直接提供所述依赖于构造的测试,一个的两种不同的方式,并通过调用TestBed.get(服务)使用依赖注入的第二个。
这两个对我来说在功能上似乎相同,除非我连续调用 TestBed.get() 时它不会在第一次调用后调用构造函数。
该角文件还提到,TestBed.get()已过时(尽管仍然指南参考吧!),我应该使用类型或InjectionToken代替,但我看不出这些类的任何可取代TestBed.get() .
在角度文档中,我看到了这两个功能,tick()和flush()。两者似乎都做类似的事情。从角度文档中,它会打勾:
为fakeAsync区域中的计时器模拟时间的异步传递。
和冲洗:
通过清空宏任务队列直到其为空来模拟fakeAsync区域中计时器的异步时间流逝。返回的值是本应经过的毫秒数。
谁能向我解释差异?
编辑(在评论中回答):
此外,在角度文档 tick()中不带参数,并且该行的注释甚至使用了“ flush”一词。
it('should display error when TwainService fails', fakeAsync(() => {
// tell spy to return an error observable
getQuoteSpy.and.returnValue(
throwError('TwainService test failure'));
fixture.detectChanges(); // onInit()
// sync spy errors immediately after init
tick(); // flush the component's setTimeout()
fixture.detectChanges(); // update errorMessage within setTimeout()
expect(errorMessage()).toMatch(/test failure/, 'should display error');
expect(quoteEl.textContent).toBe('...', 'should show placeholder');
}));
Run Code Online (Sandbox Code Playgroud)