2014-03-06 1 views
17

를 플러시 보류중인 요청을 포기하지,하지만 카르마 나에게이 오류 제공 : Error: No pending request to flush ! at Function.$httpBackend.flush

테스트

'use strict'; 

describe('profileCtrl', function() { 
var scope, $httpBackend; 

beforeEach(angular.mock.module('maap')); 
beforeEach(angular.mock.inject(function($rootScope, $controller, _$httpBackend_){ 

    $httpBackend = _$httpBackend_; 
    $httpBackend.when('GET', 'profile').respond([{id: 1, name: 'Bob'}]); 
    scope = $rootScope.$new(); 
    $controller('profileCtrl', {$scope: scope}); 
})) 

it('should fetch list of users', function(){ 
    $httpBackend.flush(); 
    expectGET(scope.current_user.length).toBe(1); 
    expect(scope.current_user[0].name).toBe('Bob'); 
}); 

}}; 이 간단한 제어기위한

:

'use strict'; 

angular.module('maap').controller('profileCtrl', function($scope, UserService) { 

$scope.current_user = UserService.details(0); 

});

+0

경고 또는 오류입니까? – madhured

+0

@madhured 여기에 같은 문제가있는 – Tres

답변

27

_$httpBackend_은 테스트에서 http 요청을하지 않으므로 아무 것도 플러시하지 않습니다.

http 요청을하는 일부 코드를 호출해야합니다.

그런 다음 어떤 곳에서 테스트에서 http 요청을 한 경우 flush 메서드를 호출하여 요청에 대한 응답이 제공되도록 할 수 있습니다.

뭔가 같은 :

경우 : 내가 만들고 있었다 요청이 예상되는 것과 다른 때문에

it('should fetch list of users', function(){ 

    // Do something that will make an http request 
    MyService.getAllUser(function ...) // for example 

    // Then provide a response for the http request that 
    // has been made when getAllUser was called 
    $httpBackend.flush(); 

    // Check the expected result. 
    expect(something).toBe('Bob'); 
}); 
8

같은 문제가 나에게 일어난 문제는 제가 요청을하지 않은 것이 아니었지만, 예를 들어 내가이 기대 정의 :

mockBackend.expectGET("http://myapi.com/001").respond("RESULT"); 

을 그리고 나는이 다른 URL 요청했다 :

http://myapi.com/002 

매우 혼란스러운 오류 메시지입니다. 아래의 문제와 관련이 없습니다.

+0

오류입니다. 요청하는 URL을 추측 할 수 없습니다. 어떻게 알았지? 로그 등을 보여줄 수있는 방법이 있습니까? – Ruben

+0

이 문제에 대해 고맙습니다. 분명히 있어야 할 때 내 코드가 http 요청을하지 않는 이유를 알아 내려고했습니다. 내가'expectPOST' url에 오타가 있었다는 것을 알았습니다. –

1

각 요청에 대한 응답을 정의하지 않았기 때문에 동일한 문제가있었습니다. 응답이 없으면 아무 것도 플러시되지 않습니다. 또한 http 약속은 결코 해결되거나 실패하지 않습니다.

0

ngMock 모듈 대신 ngMockE2E 모듈을 사용했기 때문에 동일한 예외가 발생했습니다. $ rootScope. $ digest()를 호출하는 것조차 도움이되지 않았습니다.