Angular httpBackend crashing browser

Issue

When using Jasmine and Angular (1.4.7) with Restangular (1.4.0), httpBackend and angular-mocks (1.4.7), Chrome and PhantomJS both crash when encountering the following line:

httpBackend.whenGET('/something').respond(200);

If I get rid of it entirely, as below, I get the following error:

//httpBackend.whenGET('/something').respond(200);
scope.doSomething();  // will invoke a GET to /keepAlive
httpBackend.expectGET("/something").respond(200);
httpBackend.flush();

Error: Unexpected request: GET /something
No more request expected

The code that it is testing is:

$scope.doSomething = function () {
    Restangular.one('something').get();
};

I’ve tried everything I can think of. I’m down to the idea that this is just a bug in Karma/Jasmine/Restangular but I don’t know how to work around it.

One thing worth noting is that httpBackend and Restangular disagree about the URL. Restangular adds the / to /something. httpBackend doesn’t. This is why they don’t match.

Things I’ve tried that didn’t work:

  • Calling /something from Restangular. Says unexpected GET //something
    (two slashes)
  • Using /something/another instead of a single root level
    directory. No difference.
  • using $http instead of Restangular. No difference.

Solution

Solved this a while ago. What happened is that the $httpBackend test caused a route change. That route change caused an event to fire and got us stuck in a loop.

However, this only happened when testing with $httpBackend. In the real world, this infinite loop didn’t happen.

There were two solutions:

  1. redefine $location.go in your beforeEach() so that your testing will not
    actually cause a redirect -or –
  2. capture the event somehow and squash it, or just change your code to
    not trigger it

Answered By – oooyaya

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.