Mocking $httpBackend - how to handle "Unexpected request, No more request expected"?

Askar Ibragimov picture Askar Ibragimov · Mar 14, 2014 · Viewed 57.1k times · Source

I have a Jasmine test that is coded like this:

  it ("should send correct message to server to get data, and correctly set up scope when receiving it", function(){
    $httpBackend.when('GET', 'https://localhost:44300/api/projectconfiguration/12').respond(fakedDtoBase);
    $routeParams.projectId=fakeId; // user asks for editing project
    scope.$apply(function(){
        var controller=controllerToTest(); // so controller gets data when it is created
    });
    expect(scope.projectData).toEqual(fakedDtoBase);
});

and it kind of works, but I get the error:

Error: Unexpected request: GET views/core/main/main.html
No more request expected
    at $httpBackend (C:/SVN/src/ClientApp/client/bower_components/angular-mocks/angular-mocks.js:1207:9)
    at sendReq (C:/SVN/src/ClientApp/client/bower_components/angular/angular.js:7800:9)
    at $http.serverRequest (C:/SVN/src/ClientApp/client/bower_components/angular/angular.js:7534:16)
    (more stack trace)....

I do realise that I can mock every other call. But let's say I do not care what else my test wants to load as it may call few other things. How I can make sure that every other requests just "happen silently", maybe offering a single dummy response for everything else?

Answer

PrimosK picture PrimosK · Mar 14, 2014

Your test fails because a request is made which you haven't specified.

Try to add:

$httpBackend.when('GET', 'views/core/main/main.html').respond(fakedMainResponse);

Of course you should also define fakedMainResponse.

Please take a look also at the documentation (section Request Expectations vs Backend Definitions) which says:

Request expectations provide a way to make assertions about requests made by the application and to define responses for those requests. The test will fail if the expected requests are not made or they are made in the wrong order.

The second paramete of $httpBackend.when is actually a RegExp. So if you provide a RegExp that will match all other requests it should work.