2017-12-28 58 views
0

테스트 베드를 사용하지 않고 각도를 테스트 할 때 "변경 사항을 감지 할 수 있습니까?"각도 비동기 감지 변경

구성 요소와

, 나는이있을 것이다 :

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(); 
}); 

어떻게 연결하나요? 확인해야 할 단계가 두 개인 경우 (예 : 비동기 작업 전에 한 번, 한 번만 수행, 다른 모든 것은 초기 기간 이후에 수행)?

답변

1

tick 기능을 가진 fakeAsync을 사용하면 시간주기를 시뮬레이션 할 수 있습니다.

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

일시적인 해결책으로, 때로는이 들어오는 데이터에 대한 내 통제 범위를 벗어난 사항에 의존하는 경우가 있습니다. 그래도 고마워. – Zlatko