2014-06-19 3 views
1

저장된 자격 증명을 사용하여 내 애플리케이션 로그를 작성하고 401에 대한 요청을 다시 시도해야합니다. 401을 반환하는 초기 호출이 필요하기 때문에 테스트하는 데 어려움을 겪고 있습니다. 로그인 요청이 다시 발생하면 200을 리턴하십시오. 그것이 401 번이라고 후에 나는 200 응답 활성/연습 /의 whenGET이 필요하거나 내가 원에 잡힐Angular http interceptor 테스트하기

it("should call login if the status is 401", function() { 
    $httpBackend.whenGET(api.baseUrl + "/auth/login").respond(200); 
    $httpBackend.whenGET(api.baseUrl + "/practice/active").respond(401); 

    api.practiceActiveInquery(function(result) { 
    expect(login.fn).toHaveBeenCalled(); 
    }); 

    $httpBackend.flush(); 

}); 

문제는 다음과 같습니다

지금까지 내 테스트입니다 . 아니면 HTTP 인터셉터를 완전히 잘못 테스트하는 방법에 대해 생각하고 있습니까?

답변

3

이 경우 whenGET 대신 expectGET을 사용하려고합니다. expectGET은 한 번에 하나의 요청 만 수행하므로 요청이있을 때마다 응답을 변경할 수 있습니다. 다음과 같은 내용 :

it("should call login if the status is 401", function() { 
    $httpBackend.whenGET(api.baseUrl + "/auth/login").respond(200); 
    $httpBackend.expectGET(api.baseUrl + "/practice/active").respond(401); //will return 401 the 1st time 
    $httpBackend.expectGET(api.baseUrl + "/practice/active").respond(200); //will return 200 the 2nd time 

    api.practiceActiveInquery(function(result) { 
    expect(login.fn).toHaveBeenCalled(); 
    }); 

    $httpBackend.flush(); 

});