2017-09-28 1 views
0

, 내가 사용하기 위해 약속하는 HTTP 관찰 가능한 변환하는 데이터 서비스를 비동기/AWAIT 단맛이 같은 일이 :HttpTestingController를 사용하여 약속을 테스트하는 방법은 무엇입니까? 각도 2 응용 프로그램에서

async getCustomer(id: number): Promise<Customer> { 
    return await this._http.get<Customer>(`${this.serverUrl}/customer/${id}`).toPromise(); 
} 

이 작동하고 외모와 좋은 작품 . 내가 닭이 먼저 냐 달걀이 먼저 냐의 상황에서 얻고있다

httpMock = TestBed.get(HttpTestingController); 
// ... 1 
let actual = await service.getCustomer(id); 
// ... 2 

이 뭔가를하려고 할 때

내 이전 단위 테스트는 지금, 그러나

mockBackend.connections.subscribe(c => { 
    expect(c.request.url).toBe(`${serverUrl}/customer/${id}`); 
    let response = new ResponseOptions({ body: mockResponseBody }); 
    expect(c.request.method).toBe(RequestMethod.Get); 
    c.mockRespond(new Response(response)); 
}); 

let actual = await service.getCustomer(id); 

같은 것을 함께 MockBackend을 사용 . getCustomer 메서드는 조롱 된 요청이 제공 될 때까지 반환되지 않으며 http 호출이 트리거 될 때까지 httpMock.expectOne 또는 httpMock.match을 사용할 수 없습니다.
그래서 나는 [1], 내가 기대 실패 얻고있는 httpMock 전화를 넣어 경우, 나는에 넣어 경우 [2], 나는 시간 초과 오류 :(

을 받고 있어요 주위에 방법이 있나요 이?

답변

1

나는 TestBed 정확히. 나는 그것이 getCustomer 전화가. 당신은 그렇게 할 수있는 프레임 워크를 사용해야되기 전에 모의를 설치하지 못할 않는 이유를 볼 수 없습니다 작동 방법을 모르겠어요.

하지만 현재의 설정으로 할 수 있습니다 : 약속을 기다리십시오.

httpMock = TestBed.get(HttpTestingController); 
let customerPromise = service.getCustomer(id); 
httpMock.mockRespond({}); // Or whatever 
let actual = await customerPromise; 
+0

고마워요. 그건 저를 어리석은 짓이었습니다. 킨다는 그 약속이 그 자체의 가치임을 잊어 버렸다. :) – SWeko

관련 문제