$ httpBackend.flush () throws an error if the GET request is cached

I am trying to verify that my service is using cached GET requests.

Discarded Test Version:

  var url = "/test";
  var whenTestRequest = $httpBackend.when('GET', url);

  whenTestRequest.respond(200, "r1");
  $http({ url: url, method: 'GET', cache: true }).success(function (value) {
    expect(value).toBe("r1");
  });
  $httpBackend.flush();

  whenTestRequest.respond(200, "r2");
  $http({ url: url, method: 'GET', cache: true }).success(function (value) {
    expect(value).toBe("r1");
  });
  $httpBackend.flush();

I need a second one $httpBackend.flush(), so my second function successis called, but the second call flushthrows:

Error: No pending reset request!

So I need to wrap flushin try/ catch. This works, but is there a proper way to handle this?

+4
source share
1 answer

, - $httpBackend.flush() $rootScope.$digest(). , flush , , , , http-, , .

, , HTTP- . ​​:

beforeEach(inject(function($compile, $rootScope, $httpBackend) {
  $httpBackend.expectGET('login.html').respond('<div></div>');
  $compile('<login-page/>')($rootScope);
  $httpBackend.flush();
}));

, , , . $httpBackend - , templateUrl then . , , - $http, , $httpBackend, , .

, $rootScope.$digest() promises, :

beforeEach(inject(function($compile, $rootScope) {
  $compile('<login-page/>')($rootScope);
  $rootScope.$digest();
}));

beforeEach, , .

0

All Articles